草庐IT

hex_char

全部标签

c++ - g++ 将返回的字符串文字视为 const char 指针而不是 const char 数组

当从应该使用g++(版本4.7.3)执行隐式转换的函数返回字符串文字时,我看到了一些奇怪的行为。任何人都可以解释为什么以下代码:#includeclassTest{public:templateTest(constchar(&foo)[N]){printf("Templateconstchararrayconstructor\n");}Test(char*foo){printf("char*constructor\n");}};Testfn(){return"foo";}intmain(){Testt("bar");Testu=fn();return0;}产生结果:Templateco

c++ - 我不明白在下面的代码中将 char buffer[] 与 X 类型的对象对齐的原因

Stroustrup在他的新书第151页中展示了以下使用类型说明符alignas的示例:Sometimes,wehavetousealignmentinadeclaration,whereanexpression,suchasalignof(x+y)isnotallowed.Instead,wecanusethetypespecifieralignas:alignas(T)means"alignjustlikeaT."Forexample,wecansetasideuninitializedstorageforsometypeXlikethis:voiduser(constvector

c++ - 从 *char 数组转换时有哪些严格的别名规则?

在将char数组转换为其他类型时,我对严格的别名规则感到困惑。我知道允许将任何对象转换为char数组,但我不确定反过来会发生什么。看看这个:#includeusingnamespacestd;struct{alignas(int)charbuf[sizeof(int)];//correct?}buf1;alignas(int)charbuf2[sizeof(int)];//incorrect?struct{floatf;//obviouslyincorrect}buf3;typenamestd::aligned_storage::typebuf4;//obviouslycorrecti

c# - 如何将带有 const char* 的 C union 映射到 C# 结构?

在本地库的回调函数中,我需要访问一个espeak_EVENT数组。问题出在原C代码中的UNION语句:typedefstruct{espeak_EVENT_TYPEtype;unsignedintunique_identifier;//messageidentifier(or0forkeyorcharacter)inttext_position;//thenumberofcharactersfromthestartofthetextintlength;//wordlength,incharacters(forespeakEVENT_WORD)intaudio_position;//th

c++ - 当 vector<char>&& 绑定(bind)到 vector<char>& 时编译器不会报错

我使用的是VisualStudio2013Express。classB{public:vector&a;int&b;B(vector&i,int&c):a(i),b(c){}};intmain(){intl=3;vectorh;shared_ptrbb(newB(std::move(h),l));return0;}为什么代码可以被接受?当我将参数l更改为std::move(l)时,编译器会报错“无法将参数2从'int'转换为'int&'”。 最佳答案 这是VisualC++编译器中可用的语言扩展,现在已经存在了很长一段时间。该扩展允

c++ - 使用对 const char * 的右值引用的重载解析

#includeusingnamespacestd;voidf(constchar*const&s){cout输出:rvaluervalue为什么输出不是“右值左值”? 最佳答案 字符串文字和s都不是指针(它们是数组),因此标准的相关部分是[conv.array]:Anlvalueorrvalueoftype"arrayofNT"or"arrayofunknownboundofT"canbeconvertedtoaprvalueoftype"pointertoT".Theresultisapointertothefirsteleme

c++ - 列表初始化的 char 数组是否仍然以 null 结尾?

当我阅读LippmanC++Primer(第5版,C++11)时,我遇到了这段代码:charca[]={'C','+','+'};//notnullterminatedcout在ca上调用库strlen函数,它不是以null终止的,会导致未定义的行为。Lippman等人说,“这个调用最可能的效果是strlen将继续查找跟在ca之后的内存,直到它遇到一个空字符。”稍后的练习将询问以下代码的作用:constcharca[]={'h','e','l','l','o'};constchar*cp=ca;while(*cp){cout我的分析:ca是一个非空终止的字符数组。cp是一个指向char

c++ - 带有 const char* 变量的 printf 问题

我陷入了printf问题。如果我能在这里得到一些帮助,我将不胜感激:在下面的代码中,我可以看到字体系列在第一个printf()中被正确置换,但是如果我将它设置为变量,我只会得到一个空字符串。我怎样才能把它放在一个变量中并获得正确的值?我只是不想到处输入“font.family().family().string().utf8().data()”?我用同样的方法做了这个:voidmyMethod(){constchar*fontFamily=font.family().family().string().utf8().data();//getdisplayedcorrectlyprint

C++ 将一个 int 分配给一个 char - 为什么这个工作至少没有警告?

为什么C++(可能还有C)允许我在不给我警告的情况下将和int赋值给char?可以直接赋值吗,比如inti=12;charc=i;即做一个隐式转换,或者我应该使用static_cast?编辑顺便说一句,我正在使用gcc. 最佳答案 在发明显式强制转换语法之前,C中允许使用它。然后它仍然是一种常见的做法,所以C++继承了它,以免破坏大量代码。实际上大多数编译器都会发出警告。如果没有,请尝试更改其设置。 关于C++将一个int分配给一个char-为什么这个工作至少没有警告?,我们在Stack

c++ - 这是(枚举 : char {}) a gcc bug?

在gcc-4.5下打印0,在gcc-4.6下打印1。#includeenumVenueId:char{};intmain(intargc,char**argv){VenueIdv=(VenueId)'P';std::cout 最佳答案 根据标准,VenuedId具有char类型作为基础类型,因此v应包含char'P',v=='P'应该产生true。§7.2枚举声明Eachenumerationdefinesatypethatisdifferentfromallothertypes.Eachenumerationalsohasanun