草庐IT

static_warning

全部标签

c++ - 显式构造函数和 static_cast

structFoo{explicitFoo(inta):m(a){}intpadd1,m,padd2;};voidBar(Foo){}intmain(){Bar(11);//OK,giveserrorautox=static_cast(37);x.m;}static_cast构造Foo对象是否可以,即使它的构造函数被标记为explicit?它适用于MSVC2013和GCChttp://ideone.com/dMS5kB 最佳答案 是的,static_cast将使用explicit构造函数。5.2.9Staticcast[expr.s

c++ - static_cast 与直接调用转换运算符?

考虑下面的类,作为一个简单的例子:#include#includeusingnamespacestd;classpoint{public:int_x{0};int_y{0};point(){}point(intx,inty):_x{x},_y{y}{}operatorstring()const{return'['+to_string(_x)+','+to_string(_y)+']';}friendostream&operator(p);//Option1os应该直接调用转换运算符,还是只调用static_cast并让它完成工作?这两行几乎会做完全相同的事情(即调用转换运算符),据我所

C++ 可变参数模板特化(和 static_assert)

是否可以专门化此模板声明:templateTYPEFoo(ARGS...args){static_assert(false);}我尝试了一些事情,例如:templateintFoo(floatargs){return42;}...但是当我尝试这样使用它时,我总是会遇到静态断言:autovalue=Foo(1.5f);正确的语法是什么? 最佳答案 您不得编写仅在未实例化时才有效的模板。这与标准中的以下规则相冲突:Ifnovalidspecializationcanbegeneratedforatemplate,andthattempl

c++ - 引用模板参数类型的 static_assert

我正在尝试做的是这个简单的模板钳制功能。我想确保upper>=lower在运行时和编译时。templateTclamp(constT&lower,constT&upper,constT&n){weak_assert(upper>=lower);returnstd::max(lower,std::min(n,upper));}这样写似乎合理:static_assert(upper>=lower,"invalidbounds");但是,当使用非constexpr调用时参数,编译器给我这个:Static_assertexpressionisnotanintegralconstantexpre

c++ - 你能初始化 unique_ptr 的 "static const vectors"吗? (C++17 与 GCC 7.3)

我正在尝试创建一个staticconst默认对象(规则)的列表太大而不能经常复制,因此我想将它们存储在vector中的unique_ptr.我注意到类似的问题已经进行了几次,但我不清楚这是否真的可行(我倾向于不可行)。即你不能使用initializer_list与unique_ptr因为对成员的访问是const导致复制操作。您不能通过引用传递临时变量,从而导致复制操作。因此两者:staticconststd::vector>kStrings={std::unique_ptr(newstd::string("String1")),std::unique_ptr(newstd::strin

C++ 删除 static_cast<void*> (指针)行为

假设代码执行以下操作:T*pointer=newT();deletestatic_cast(pointer);结果是什么?未定义,内存泄漏,内存被删除? 最佳答案 行为未定义。关于delete表达式,C++标准说:Inthefirstalternative(deleteobject),ifthestatictypeoftheoperandisdifferentfromitsdynamictype,thestatictypeshallbeabaseclassoftheoperand’sdynamictypeandthestaticty

c++ - 杂乱的函数指针 : how to remove the warning?

正如我在thispost中提问和回答的那样.我有以下示例代码。#includecharfoo(){return'a';}charbar(){return'b';}charblurga(){return'c';}charbletch(){return'd';}char(*gfunclist[])()={foo,bar,blurga,bletch};char(*(*x())[])(){staticchar(*funclist[4])()={foo,bar,blurga,bletch};returnfunclist;}intmain(){printf("%c\n",gfunclist[0](

c++ - 关于static_cast的问题

我写了一段代码,但我对它的输出感到困惑:#includeusingnamespacestd;classB{public:virtualvoidfoo(){cout(pb);pd1->foo();pd1->disp();}intmain(intargc,char*argv[]){B*pb=newB();func(pb);return0;}输出是:B::fooD::disp但是据我所知,pb指向类型B。而且里面没有名为disp()的函数?那么,为什么它可以访问D类中的disp()函数? 最佳答案 因为disp()不访问类的任何成员,原则

c++ - 对模板中的所有其他类型执行 static_assert

如何对模板中的所有其他类型执行static_assert(或其他检查)?template//T1,T2,T3,...structfoo{//HowcanI//forT1,T3,T5,T7,...//dosomechecks,forexample://static_assert(std::is_default_constructible::value,"invalidtype");//static_assert(std::is_copy_constructible::value,"invalidtype");}; 最佳答案 请试试这个

c++ - "#pragma GCC diagnostic warning"与海湾合作委员会

这个问题在这里已经有了答案:HowcanIdisable#pragmawarnings?(5个答案)关闭9年前。我使用GCC4.5.1并收到如下警告:warning:expected[error|warning|ignored]after'#pragmaGCCdiagnostic'原因是“#pragmaGCCdiagnosticpush”,对于4.6版之前的GCC不存在。我不能更改代码(这不是我的)和GCC版本。我怎样才能禁用这些警告?一些GCcflags可能是?P.S.:我看到Why"pragmaGCCdiagnosticpush"popwarninginGCC/C++?,但我的问题