草庐IT

static-initializer

全部标签

c++ - gcc 与 clang : noexcept parsed in unused template specialization when static casting

我正在尝试将函数指针静态转换为特定函数重载,但似乎clang仍会解析(未使用的)模板特化的noexcept语句,从而生成编译器错误。如果未使用相应的函数重载,GCC似乎并不关心noexcept。templatevoidfun(T)noexcept(T(1)){}voidfun(int){}voidfun(int*){}intmain(){inta;fun(&a);//callingworksfinefun(a);static_cast(&fun);//staticcastingdoesn't}https://godbolt.org/z/ixpl3f这里是哪个编译器出错了?当将函数指针转

c++ - 带有转换运算符的类上的 static_cast

我刚刚遇到这种行为,我很难理解为什么这不起作用。enumclassTestEnum{Foo,Bar};classMyClass{public:operatorTestEnum(){returnm_enum;}TestEnumm_enum=TestEnum::Foo;}MyClasstheClass;intenumValue=static_cast(theClass);//doesnotwork,conversionoperatornotcalledintenumValue=static_cast(static_cast(theClass))//worksasexpected我知道编译器

c++ - reinterpret_cast<int*>(char*) 与 static_cast<int*>(static_cast<void*>(char*)) - 使用哪个?

当你动态分配了一个char*类型的缓冲区并想将它转换为特定类型时,你是否应该使用类似的东西reinterpret_cast(char*)或者类似的东西static_cast(static_cast(char*))为什么?我个人很想使用后者,因为对我来说,它并不是真正的数据“重新解释”(而只是一种分配缓冲区的机械方式)而且它看起来不像是一个来源错误的方式可能与典型的reinterpret_cast相同,但这是正确的直觉吗? 最佳答案 AccordingtoDaveAbrahams,使用链式static_casts是强制指针类型的正确、

c++ - static_cast 和临时创建(最终版)

先决条件:要理解这个问题,请先阅读以下问题及其答案:Castauto_ptrtoauto_ptr在Castauto_ptrtoauto_ptr史蒂夫回答说,“您的static_cast会将auto_ptr复制到一个临时文件,因此aS将被重置,当临时文件(在语句末尾)时,资源将被销毁。”我对static_cast时临时创建的过程很感兴趣叫做。我想要我可以跟踪的代码以查看此效果。我不能使用static_cast>...因为它不能被编译,所以我需要写一些模拟类而不是auto_ptr并观看临时创建的过程。我也明白临时创建与复制构造函数调用密切相关。auto_ptr的所有权丢失是通过设置_rad

C++ 2011 : good syntax to initialize an array in a constructor?

这里我有一个示例类:templateclassMyClass{public:MyClass();~MyClass();protected:T_data[SIZE];};templateMyClass::MyClass()://_data()OR_data({})OR_data{}OR...{;}在默认构造函数中将整个数组初始化为0的正确C++2011语法是什么?非常感谢。 最佳答案 统一初始化语法运行良好:MyClass():_data{}{} 关于C++2011:goodsyntaxt

c++ - 为什么要在 Py_Initialize() 之前调用 PyImport_AppendInittab()?

根据documentation,PyImport_AppendInittab“应该在Py_Initialize()之前调用。”没有解释为什么会这样,忽略此建议会产生一个有效的应用程序。那么,既然可以,那么在什么情况下会不行呢? 最佳答案 因为文档是这么说的;违反API可能会在今天产生一个可用的应用程序,但明天就不会。您可能会遇到的几个问题:sys.builtin_module_names在Py_Initialize中初始化,因此它不会包含您的模块PyImport_AppendInittab不获取任何锁,因此如果您在多线程应用程序中的

c++ - 为什么我收到错误 : initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?

我是C++的新手,在盯着它看了太久之后终于放弃了尝试编译它。编译器似乎出于某种原因拒绝了头文件中的构造函数原型(prototype)......我无法弄清楚它有什么问题。项目.h:#ifndefITEM_H_#defineITEM_H_classItem{public:Item(int);//ThislineiswhatEclipsekeepsflaggingupwiththeerrorinthetitlevirtual~Item();Item*getNextPtr();intgetValue();voidsetNextPtr(Item*);};#endif/*ITEM_H_*/在我的

c++ - 警告 : array 'alphabet' initialized by parenthesized string literal

在模板函数中,我目前有以下行:staticconstunsignedcharalphabet[17]=(Uppercase)?("0123456789ABCDEF"):("0123456789abcdef");其中Uppercase是模板参数。使用-pedanticgcc告诉我:warning:array'alphabet'initializedbyparenthesizedstringliteral'("0123456789abcdef")'如何摆脱该消息(我希望alphabet位于堆栈中)? 最佳答案 使代码明确有效的最简单方法

c++ - 是数组 a<T, N> = { initializer-list };正确的语法?

在工作草案中http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3225.pdf23.3.2它说Anarrayisanaggregate(8.5.1)thatcanbeinitializedwiththesyntaxarraya={initializer-list};我会通过arraya={initializer-list};是正确的,有人可以解释一下这种奇怪的语法吗? 最佳答案 草稿有误。C++11标准的最终版本有arraya如您所料。

c++ - 警告 : non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

这个问题在这里已经有了答案:C++ArrayInitializersWarnings(2个答案)关闭8年前。我刚刚在我的机器上安装了最新版本的cygwin和eclipseluna。它工作正常,我能够运行我的项目。但是,当我构建它们时,我收到了我不明白的警告。例如,这是我从“c++Primer”一书的网站上获得的头文件“Sales_item.h”的警告:warning:defaultedanddeletedfunctionsonlyavailablewith-std=c++11or-std=gnu++11[enabledbydefault]Sales_item()=default;^..