草庐IT

template_const

全部标签

C++11 : Is it possible to give fixed-template-parameted template to varidic-template-template-parameter?

(是的,由于我糟糕的英语,标题很奇怪;我希望有人能改进它。)接听thisquestion,我发现这段代码有效:templateclassA{};templateclassU>classB{};intmain(){Bit_works;}..虽然templateclass和templateclass不相等。我试图弄清楚为什么这是可能的,并观察了N3337standard的[temp.param],但我找不到任何东西。怎么可能? 最佳答案 是的,这是可能的。C++1114.3.3/3特别允许,并提供了一个例子。3Atemplate-arg

c++ - 通过 const reference 或 by value 传递 int,有什么区别吗?

这个问题在这里已经有了答案:Isitcounter-productivetopassprimitivetypesbyreference?[duplicate](7个答案)关闭7年前。当我将int和double之类的原语传递给函数时,是通过constreference传递它们更好,还是通过值传递它们更好(假设我不更改变量的值)?intgetValueFromArray(intindex){//returnthevaluefromthearray}intgetValueFromArray(constint&index){//returnthevaluefromthearray}谢谢

c++ - 是使用T const&还是T&&

我很好奇,一般来说,您是否要对从C++11开始的模板化函数参数使用T&&(通用引用)而不是经典的Tconst&(左值引用)。我特别好奇的是,如果您还想处理r值引用,您将如何避免被迫丢失const的事实;有办法解决这个问题吗? 最佳答案 “丢掉const”是没有问题的。如果参数是cv限定的,则T将使用cv限定符推导。例如,如果参数是conststd::string类型的右值,则T将被推导为conststd::string。您不可能通过使用转发引用来违反常量正确性。如果可以,那将是一个主要的语言缺陷。至于何时应使用转发引用的更广泛问题,

c++ - 在编译时填充 std::array 和 const_cast 可能的未定义行为

已知std::array::operator[]由于C++14是constexpr,请参见下面的声明:constexprconst_referenceoperator[](size_typepos)const;但是,它也是const限定的。如果您想使用std::array的下标运算符以便在编译时为数组赋值,这会产生影响。例如考虑以下用户文字:templatestructFooLiteral{std::arrayarr;constexprFooLiteral():arr{}{for(inti(0);i如果您尝试声明类型为FooLiteral的constexpr变量,上述代码将无法编译。这

c++ - 使用 const 键但非 const 值进行映射?

我有一种情况,我想要一个不允许在初始化后添加/删除键的映射,但允许更改值(因此我不能简单地使映射const).即/*semi-const*/mapmyMap=initMap();myMap[1]=2;//NOTOK,becausepotentiallyaddsanewkeymyMap.at(1)=2;//OK,becauseworksonlyifkeyispresentfor(auto&element:myMap){element.second=0;//OK,valuesmaychange}我可以为std::map编写我自己的包装器,但我觉得这并不少见,所以我想知道是否已经存在解决方案

c++ - 'const' 是否取消了通用引用的资格?

我有一个使用通用引用的ctor的人类类classHuman{public:templateexplicitHuman(T&&rhs){//dosomeinitializationwork}Human(constHuman&rhs);//thedefaultctorIdon'tcareabout}现在如果我有一个constHuman对象constHumanone_I_do_not_care;//thenplaywiththatHumanthe_human_I_care(one_I_do_not_care)//nowcreateanotherone最后一行是使用模板构造函数还是默认构造函数

c++ - 如何使用 const 键复制 unordered_map?

简单代码:#includeintmain(){std::unordered_mapm;std::unordered_mapm1=m;}产生复杂的编译错误信息:ErrorC2280'std::hash::hash(void)':attemptingtoreferenceadeletedfunction在其内部基本上说unordered_map并不期望key是常量附言:我读过answer对于类似的问题:Theassociativecontainersonlyexposethe(key,value)pairasstd::pair,sotheadditionalconstonthekeytyp

c++ - 为什么重写方法并将 const 添加到参数类型有效?

这个问题在这里已经有了答案:FunctionswithconstargumentsandOverloading(3个答案)关闭3年前。考虑以下示例:#include#includeclassBase{public:virtualvoidfunc(inta){}};classDerived:publicBase{public:voidfunc(constinta)override{}};intmain(){Derivedd;d.func(1);return1;}我重写了func方法,但将const添加到参数中,在这种情况下,链接器应该尖叫说出现了问题。要么函数没有被覆盖,要么函数参数不应

c++ - 奇怪的 "Could not deduce template argument for ' T'"错误

错误在this代码://myutil.htemplateTConditionalInput(LPSTRinputMessage,LPSTRerrorMessage,predicatecondition);//myutil.cpptemplateTConditionalInput(LPSTRinputMessage,LPSTRerrorMessage,Predcondition){Tinputcout>input;while(!condition(input)){cout>input;}returninput;}...//c_main.cppintrow;row=ConditionalI

c++ - const 双指针参数的非常量指针参数

C++中star之前的const修饰符意味着使用这个指针指向的值不能改变,而指针本身可以指向别的东西。在下面voidjustloadme(constint**ptr){*ptr=newint[5];}intmain(){int*ptr=NULL;justloadme(&ptr);}juSTLoadme函数不允许编辑传递的参数指向的整数值(如果有的话),但它可以编辑int*值(因为const不在第一个星之后),但为什么我在GCC和VC++中都会遇到编译器错误?GCC:错误:从int**到constint**的无效转换VC++:错误C2664:“juSTLoadme”:无法将参数1从“in