草庐IT

non-void

全部标签

c++ - 将 (void*) 转换为 std::vector<unsigned char>

我有一个(void*)我需要转换为std::vector的缓冲区在我可以传递它之前。不幸的是,我的C++类型转换技能有点弱。有什么建议吗? 最佳答案 您将需要缓冲区的长度。一旦你这样做了,我们就可以这样做:unsignedchar*charBuf=(unsignedchar*)voidBuf;/*createavectorbycopyingoutthecontentsofcharBuf*/std::vectorv(charBuf,charBuf+len);好的,评论让我开始了解我为什么不使用reinterpret_cast:InC+

c++ - 为什么我可以将 int 和 BOOL 强制转换为 void*,但不能 float ?

void*是C和衍生语言的一个有用特性。例如,可以使用void*将Objective-C对象指针存储在C++类中。我最近在做一个类型转换框架,由于时间限制有点懒——所以我使用了void*...这就是这个问题的来历:为什么我可以将int类型转换为void*,但不能将浮点类型转换为void*? 最佳答案 BOOL不是C++类型。它可能是typedef或在某处定义的,在这些情况下,它与int相同。例如,Windows在Windef.h中有这个:typedefintBOOL;所以你的问题归结为,为什么你可以将int类型转换为void*,但不

c++ - 为什么我可以将 int 和 BOOL 强制转换为 void*,但不能 float ?

void*是C和衍生语言的一个有用特性。例如,可以使用void*将Objective-C对象指针存储在C++类中。我最近在做一个类型转换框架,由于时间限制有点懒——所以我使用了void*...这就是这个问题的来历:为什么我可以将int类型转换为void*,但不能将浮点类型转换为void*? 最佳答案 BOOL不是C++类型。它可能是typedef或在某处定义的,在这些情况下,它与int相同。例如,Windows在Windef.h中有这个:typedefintBOOL;所以你的问题归结为,为什么你可以将int类型转换为void*,但不

c++ - 如何使用 atexit() 注册非 void 函数?

我正在尝试注册一个函数,该函数返回一个int以在程序结束时使用atexit()函数调用。(特别是ncurses中的endwin()函数。)但是由于atexit()需要一个指向void函数的指针,我遇到了一个问题。我尝试了以下方法:static_cast(endwin)但似乎不允许从int函数到void函数的static_cast。我想要实现的目标是否可行,如果可以,如何实现?注意:我愿意忽略函数的返回值。编辑:我还尝试创建一个lambda函数,这似乎可以满足我的要求:atexit([]{endwin();});与包装/转发功能相比,这是一个好的解决方案吗?(除了它需要C++11并且避免

c++ - 如何使用 atexit() 注册非 void 函数?

我正在尝试注册一个函数,该函数返回一个int以在程序结束时使用atexit()函数调用。(特别是ncurses中的endwin()函数。)但是由于atexit()需要一个指向void函数的指针,我遇到了一个问题。我尝试了以下方法:static_cast(endwin)但似乎不允许从int函数到void函数的static_cast。我想要实现的目标是否可行,如果可以,如何实现?注意:我愿意忽略函数的返回值。编辑:我还尝试创建一个lambda函数,这似乎可以满足我的要求:atexit([]{endwin();});与包装/转发功能相比,这是一个好的解决方案吗?(除了它需要C++11并且避免

c++ - 为什么 void_t<> 检测习语不适用于 gcc-4.9?

这个问题在这里已经有了答案:C++11:SFINAEintemplateparameters,GCCvsClang[duplicate](1个回答)关闭6年前。考虑以下代码:#include#includestructTest{Test&operator++();};structNoIncrement{};templateusingvoid_t=void;template>structhas_pre_increment_member:std::false_type{};templatestructhas_pre_increment_member())>>:publicstd::true

c++ - 为什么 void_t<> 检测习语不适用于 gcc-4.9?

这个问题在这里已经有了答案:C++11:SFINAEintemplateparameters,GCCvsClang[duplicate](1个回答)关闭6年前。考虑以下代码:#include#includestructTest{Test&operator++();};structNoIncrement{};templateusingvoid_t=void;template>structhas_pre_increment_member:std::false_type{};templatestructhas_pre_increment_member())>>:publicstd::true

c++ - 将新放置到缓冲区后,缓冲区和实例是否具有相同的 void* 地址?

请看下面的代码:unsignedchar*p=newunsignedchar[x];CLASS*t=new(p)CLASS;assert((void*)t==(void*)p);我可以假设(void*)t==(void*)p吗? 最佳答案 是的,你可以。我相信有几条规定可以保证这一点。[expr.new]/10-强调我的Anew-expressionpassestheamountofspacerequestedtotheallocationfunctionasthefirstargumentoftypestd::size_t.Tha

c++ - 将新放置到缓冲区后,缓冲区和实例是否具有相同的 void* 地址?

请看下面的代码:unsignedchar*p=newunsignedchar[x];CLASS*t=new(p)CLASS;assert((void*)t==(void*)p);我可以假设(void*)t==(void*)p吗? 最佳答案 是的,你可以。我相信有几条规定可以保证这一点。[expr.new]/10-强调我的Anew-expressionpassestheamountofspacerequestedtotheallocationfunctionasthefirstargumentoftypestd::size_t.Tha

c++ - Visual Studio 2015 “non-standard syntax; use ' &' to create a pointer to member”

我正在学习C++并尝试制作一个小游戏井字游戏。但我不断得到C3867,非标准语法;使用'&'创建一个要记住的指针。这是我的井字游戏.h:#pragmaonce#includeusingnamespacestd;classTicTacToe{public:TicTacToe();stringgetName1();stringgetName2();voidprintBoard();voidclearBoard();voidsetName1(stringplayer1Name);voidsetName2(stringplayer2Name);voidsetSign1(stringplayer