草庐IT

TEMPLATE

全部标签

c++ - 获取模板参数的 decltype

我经常想要获取类模板参数的decltype以便进一步使用它,就像在我已经剥离和简化以显示我的问题的循环中:templateclassFoo{public:Ttype;//ThisismyhacktogetdecltypeofT};templateclassBar{public:};intmain(){for(auto&foo:fs){//IstheresomewaytogetthedecltypeofthetemplatewithouthavingtorefertosomearbitraryTmemberofFoo?autobar=someFunction();}}有没有办法在不执行此

c++ - 避免运行时到编译时数字参数转换的代码重复

假设我们有这样的函数templatevoidfoo();为简单起见,假设我们知道只有(常数)值N_1、N_2...N_k对N有效。现在,假设我想将该编译时参数设为运行时参数,使用foo()作为黑盒,即实现:templatevoidfoo(unsignedn);通过调用foo()。我应该怎么做?显然,我可以写:templatevoidfoo(unsignedn){switch(n){caseN_1:foo();break;caseN_2:foo();break;//etc.etc.caseN_k:foo();break;}}...但这让我觉得很脏。我想我可以使用MAP()元宏来生成这些k

C++ 什么时候可以扩展 `std` 命名空间?

SO上的一个线程说extendingstdisUB(好吧,当然除非你是标准的作家)。但时不时地,std会被愉快地扩展。什么时候可以这样做? 最佳答案 可以将定义添加到std命名空间的唯一情况是对命名空间中已存在的模板进行特化,并显式实例化一个模板。但是,仅当它们依赖于用户定义的类型时。[namespace.std](标准草案):ThebehaviorofaC++programisundefinedifitaddsdeclarationsordefinitionstonamespacestdortoanamespacewithinna

c++ - 为什么模板类的成员需要通过其模板类的参数进行参数化

在Stroustrup的书(第4版-首次打印)的第668页,您会找到以下模板类示例吗?templateclassString{public:String();...private:intsz;C*ptr;};作者在第679页写道:Membersofatemplateclassarethemselvestemplatesparameterizedbytheparametersoftheirtemplateclass.Whensuchamemberisdefinedoutsideitsclass,itmustexplicitlybedeclaredastemplate.Forexample

c++ - reference_wrapper : make_pair VS Class Template Argument Deduction (CTAD)

为什么make_pair和类模板参数推导(CTAD)不同意生成哪种类型?#include#include#include#includeintmain(){intmyInt=5;std::reference_wrappermyIntRef=myInt;automyPair=std::make_pair(myInt,myIntRef);std::pairMy2ndPair(myInt,myIntRef);std::cout输出:St4pairIiRiE//std::pairSt4pairIiSt17reference_wrapperIiEE//std::pair>更新:为什么std::p

c++ - 为什么转发引用不是常量?

转发引用应该将参数转发给另一个函数,对吧?那为什么它不是const呢?templatevoidfunc(constT&&);非常量引用允许函数修改其参数(而不是仅仅转发它们)。 最佳答案 Whyisn'tforwardingreferenceconst?因为希望能够移动一个完美转发的xvalue。通常不能从const引用移动对象,因为移动构造函数的参数需要是非常量。此外,希望能够将左值绑定(bind)到转发引用中。不可能将左值绑定(bind)到constT&&中,这是一个const右值引用-它根本不是转发引用1。如果您不想离开论点,

C++如何创建一个自动转换图?

假设我们有一个库A,其中包含两类对象C1和C2。对于C1中的每个元素,在C2中都有一个兼容的元素。假设我们要创建一个通用函数,将一个函数转换为另一个函数。templateC2SpecialCast(C1c1){/*dosomegenericwork*/}这需要我们一直写SpecialCast(parameter)然而这是多余的,因为知道转换类型所需的只是知道参数类型。因此,这给代码增加了不必要的冗长。理想的解决方案是定义一个有效类型转换的头表,这样就可以做到:templateC2SpecialCast(C1c1){/*getC2fromC1usingthetable*//*dosome

c++ - 使用点后模板函数的特化会破坏编译

考虑下一个例子:#includetemplatevoidfoo();intmain(intargn,char*argv[]){foo();}templatevoidfoo(){std::cout编译失败并显示以下错误消息:rg.cpp:12:error:specializationof‘voidfoo()[withinta=1]’afterinstantiation标准中的哪一段解释了这个错误?PS:我知道如果我将函数定义移到main前面将使错误消失。 最佳答案 根据标准,我认为这是未定义的行为。在UB的情况下,工具链可以做什么没有

c++ - 模板函数的特化可以是虚拟的吗?

比如,classA{templateTDoStuff();templatevirtualintDoStuff()=0;};VisualStudio2010说不,但我有一种奇怪的感觉,我只是搞砸了语法。成员函数模板的显式完全特化可以是虚拟的吗? 最佳答案 在类中显式特化是不合法的。即使您可以使其部分特化,您仍然会遇到“模板不能是虚拟的”问题。n3290,§14.5.2指出:Amemberfunctiontemplateshallnotbevirtual.并给出这个例子:templatestructAA{templatevirtualv

c++ - 嵌套模板特化结果为 "Illegal use of explicit template arguments"?

如何专门化嵌套模板?(请参阅下面的错误。)usingstd::reverse_iterator;templatereverse_iteratormake_reverse_iterator(constIt&it){returnreverse_iterator(it);}templateItmake_reverse_iterator>(constreverse_iterator&it){//Above^//errorC2768://'make_reverse_iterator':illegaluseofexplicittemplateargumentsreturnit.base();}