草庐IT

static-variables

全部标签

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++ - 警告 : Unreferenced local variable

当我为类A提供构造函数时,我没有得到未引用的局部变量,为什么?空构造函数如何消除警告?classA{public:A(){}};intmain(){Aa;} 最佳答案 这只是一种理论,但由于构造函数可能包含可能导致副作用的代码,因此有人可能会决定构造一个未使用的对象来运行该代码。如果您没有构造函数并且从不引用您构造的对象,那么可以安全地确定该对象没有任何用途。 关于c++-警告:Unreferencedlocalvariable,我们在StackOverflow上找到一个类似的问题:

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++ - std::static_pointer_cast 是否有任何额外的运行时开销?

相对于static_cast,即。所以,如果我们有这两个类型转换Base*b(newDerived());Derived*d=static_cast(b);//(1)shared_ptrb(newDerived());shared_ptrd=static_pointer_cast(b);//(2)第(2)行会比第(1)行慢吗? 最佳答案 是的,它有更多的开销,因为它必须返回一个新的shared_ptr而不是一个新的原始指针。boost实现是:templateshared_ptrstatic_pointer_cast(shared_p

c++ - Static Cast 访问静态 const 类成员

这个问题在这里已经有了答案:Undefinedreferencetostaticconstint(9个回答)关闭5年前。所以昨天我正在寻找SO,但找不到以下问题的答案。这种情况来self正在使用的一些代码,但这里是MCVE来演示它。我在A.h中定义了一个类A,其中只有一个静态常量。我已经在标题中对其进行了初始化。#ifndefA_H_#defineA_H_classA{public:staticconstinttest=5;~A(){};};#endif/*A_H_*/然后我有一个B类需要从A类访问publicstaticconst。在这个例子中,它将把值深度复制到一个vector中。