草庐IT

TEMPLATE

全部标签

c++ - 右值引用或转发引用?

我知道对于下面的函数templatevoiddo_something(T&&arg);函数参数是转发引用。但是在下面的情况下它仍然是转发引用还是右值引用?templateclassMyClass{voiddo_something(T&&arg);};我认为它仍然是转发引用,但我不确定。此外,我想知道如果结果不是我想要的,可以做什么来强制执行右值引用或转发引用。 最佳答案 这是一个右值引用。转发引用只能出现在推导的上下文中。这只是一个成员函数,它接受对class模板参数的右值引用。如果要维护函数的模板参数推导,则不能强制转发引用为右值

c++ - SFINAE 构造函数

这个问题在这里已经有了答案:SFINAEworkinginreturntypebutnotastemplateparameter(3个答案)Templatespecializationandenable_ifproblems[duplicate](1个回答)关闭4年前。我一直喜欢这样的SFINAE函数语法,似乎通常运行良好!template::value>::type>T(Integern){//...}但是当我想在同一个类(class)也这样做时遇到了问题......template::value>::type>T(Floatn){//...}出现这样的错误:./../T.h:286

C++ 编译时子串

我有非常大的代码库,它广泛使用__FILE__进行日志记录。但是,它包含完整路径,这是(1)不需要的,(2)可能会违反安全规定。我正在尝试编写编译时子字符串表达式。结束了thissolutionstaticconstexprcstrPastLastSlash(cstrstr,cstrlast_slash){return*str=='\0'?last_slash:*str=='/'?PastLastSlash(str+1,str+1):PastLastSlash(str+1,last_slash);}staticconstexprcstrPastLastSlash(cstrstr){re

调用模板化内部类静态成员函数的 C++ 语法?

我有一些模板代码可以在VC9(MicrosoftVisualC++2008)中正常编译,但不能在GCC4.2(在Mac上)中编译。我想知道我是否缺少一些语法魔法。下面我有一个精简的例子来证明我的错误。抱歉,如果这个示例看起来毫无意义,我已尽可能删除以隔离此错误。特别是我有一个模板类S,它有一个内部类R,它也是一个模板类。从顶级模板函数foo,我试图调用R::append,它是R的静态成员函数:templatestructS{templateS&append(constT&){return*this;}templatestructR{templatestaticS&append(S&s,

C++模板特化方法定义

下面的代码工作正常,一个简单的模板类,有定义和使用#include#includeusingnamespacestd;templateclassfoo{public:stringwhat();};templatestringfoo::what(){return"foooftypeT";}intmain(){foof;cout如果我然后添加以下内容(在main之上,但在模板类foo声明之后;)templateclassfoo{public:stringwhat();};templatestringfoo::what(){return"foooftypechar";}我从g++得到一个错误

指向类成员的模板函数的C++指针

我在“C”类中有以下函数classC{templatevoidFunc1(intx);templatevoidFunc2(intx);};templatevoidC::Func1(intx){Ta(x);}templatevoidC::Func2(intx){Ta(x);}函数仅在实现中使用模板。签名不包含模板参数。是否可以定义指向此类模板函数的指针?我尝试了以下定义,但它导致编译错误。typedeftemplatevoid(СSomeClass::*TFuncPtr)(int); 最佳答案 一旦实例化,成员函数模板就是一个普通的成

c++ - g++ 可变参数模板。简单示例代码无法编译,提示 'Not a template'

我正在探索这个陌生的领域,并想尝试来自DannyKalev'stutorialonthematter的一个简单示例.代码非常简单:templatestructCount{staticconstintvalue=0;};templatestructCount//partialspecialization{staticconstintvalue=1+Count::value;};但是gcc4.4.7甚至4.7.0提示(尽管-std=c++0x-std=gnu++0x标志):/src/tests/VTemplates.h:12:8:error:'Count'isnotatemplate/sr

c++ - 如何获得与函数一起使用的模板模板参数推导?

考虑一组函数,例如templatevoidA(constFun&){}templatevoidB(constFun&){}templatevoidC(constFun&){}旨在将函数类型作为参数。然后,这完全没问题:templatevoidFunc(constT&){}A(Func);B(Func);C(Func);现在我想摆脱重复inttemaplate参数,所以我尝试了这个:templatestructHelper{templateclassFun>staticvoidA(Fun&f){A(f);}templateclassFun>staticvoidB(Fun&f){B(f);

c++ - 错误 C2899 : typename cannot be used outside a template declaration

我正在MSV2010中尝试以下内容namespacestatismo{templatestructRepresenterTraits,3u>>{typedefitk::Image,3u>VectorImageType;typedefVectorImageType::PointerDatasetPointerType;typedefVectorImageType::PointerDatasetConstPointerType;typedeftypenameVectorImageType::PointTypePointType;typedeftypenameVectorImageType:

C++ 将模板类型名类作为函数参数传递

我需要将模板类作为参数传递给函数,但我可以在函数内检索类型名称以初始化时间变量类声明如下:templateclassListIndex_Linked这里是类在main中的初始化和函数的调用ListIndex_LinkedL;insertion(L);以及我正在尝试做的事情template>voidinsertion(list&L){Type&temp=L.get(0);{intmax=L.numElem();for(inti=1,;i但是我得到这个错误:error:'list'isnotatemplatevoidinsertion(list&L)^在此先感谢您的帮助