草庐IT

TEMPLATE

全部标签

部分特化时基于 C++ 模板的 "override"等效?

我有一个看起来像这样的模板类/结构:templatestructS{unsignedintoperator()(Tt,Uu)const;};我想确保特化遵循这个接口(interface)。不幸的是,我可以用不同的返回类型专门化这个结构。例如,如果我部分专门化返回bool而不是unsignedint,我预计会出现编译器错误,但编译器似乎并不关心:templatestructS{booloperator()(Tt,nullptr_tu)const{return2;}};Example@Ideone.com在上面的例子中,专用版本应该返回2但由于返回类型是bool,返回值被转换为true然后

c++ - 从 enable_if 基础继承

我正在尝试为非字符数组部分特化一个特征:templatestructis_container:std::false_type{};templatestructis_container:std::enable_if::value,std::true_type>::type{};VisualStudio2010给了我一个C2039(type不是enable_if的元素...)。但是,SFINAE不应该在这里触底而不是给出编译器错误吗?或者SFINAE不适用于这种情况?当然,我可以将非字符和字符的特化分开:templatestructis_container:std::false_type{

c++ - 非类型模板参数的类型抽象

我想编写一个模板,该模板可以将类型解构为具有非类型模板参数及其非类型模板参数的模板。例如,它将Array解构为templateArray和5,但通常可用于任何类型的非类型模板参数(整数类型,指针,成员指针等)。首先尝试使用模板特化:templatestructfoo{enum{n=1};};templatestructbar{enum{n=x};};templateclassX,Tx>structfoo>{enum{n=x};};//herexmustbeofintegraltype,butthat'sjustfortestingintmain(int,char**){returnfo

c++ - 有没有办法推断函数指针模板参数的值?

C++允许非类型模板参数为指针,包括函数指针、类型。我最近问了一个question关于这有什么用,这是对oneoftheanswers的跟进.是否可以从作为相关函数指针的函数参数中推断出函数指针模板参数的值?例如:usingVoidFunction=void(*)();templatevoidtempl(VoidFunction);...voidfunc();//aVoidFunction...templ(func);//works,butIhavetospecifythetemplateparameterexplicitlytempl(func);//有没有办法让这种扣除发生?从编译

c++ - 在仅 header 库的类定义中实现有什么不同?

我正在开发一个大量使用模板的库,因此我决定将其设为仅包含header的库。由于声明和实现在同一个文件中,我现在可以同时进行。所以我可以在这两种风格之间做出选择://seperatedeclarationandimplementationtemplateclassKlass{public:voiddo_something();};templatevoidKlass::do_something(){//dosomething}//orbothatthesametimetemplateclassKlass{public:voiddo_something(){//dosomething}};我

c++ - C++ 模板声明中的范围和默认参数 : Clarifying Standardese

我正在阅读C++14standard's模板部分试图提高我对主题的理解,并偶然发现了这个特定规则:§14.112Atemplate-parametershallnotbegivendefaultargumentsbytwodifferentdeclarationsinthesamescope.[Example:templateclassX;templateclassX{/∗...∗/};//error—endexample]根据我(相对不知情的)阅读,“相同范围”的规范意味着能够在定义模板的不同范围内声明模板。根据thisarticleonDr.DobbsC++identifiesfi

声明模板类成员和构造函数时的 C++ 习语

虽然以下两个编译(使用VisualStudio2013),但其中一个是否更“正确”关于C++习语?在调用基类构造函数和声明成员时,我特别提到了显式模板参数。TheStandard对此有何看法?是否有充分的实际理由优先选择其中一个?templateclassBar1:publicBase{public:Bar1():Base(){}Bar1(Tvalue):Base(value){}Bar1(Bar1const&other):Base(other.value){}voidFoo(Bar1const&other){//Somefoorelatedactivity.}};templatecl

c++ - 为什么 injected-class-name 有时不被视为类模板中的模板名称?

SourceInthefollowingcases,theinjected-class-nameistreatedasatemplate-nameoftheclasstemplateitself:itisfollowedbyitisusedasatemplateargumentthatcorrespondstoatemplatetemplateparameteritisthefinalidentifierintheelaboratedclassspecifierofafriendclasstemplatedeclaration.所以我尝试检查所有3种情况(另外在基本歧义的情况下,尽管我

c++ - 使用来自另一个模板的参数对模板进行柯里化(Currying)

我有类Foo,它有两个模板参数,A和B:templatestructFoo{};我还有类Base,它有一个模板模板参数:templatetypenameFoo>structBase{};我想编写类Derived假设如下:Derived有一个模板参数(A)DerivedextendsclassBaseDerived作为模板参数传递给类Base类Foo,但带有一个参数“currying”(A)我该怎么做?这是我的(notworking)解决方案:templatetypenameFoo>structBase{};templatestructFoo{};templatetypenameFoo,

C++ 在成员函数范围内使用语句

如果我想使用来自模板派生类的模板基类的成员,我必须将它引入范围:templatestructbase{voidfoo();};templatestructderived:base{usingbase::foo;};为什么我不能像其他using语句一样将此using语句放入本地范围?templatestructbase{voidfoo();};templatestructderived:base{voidf(){usingbase::foo;//ERROR:baseisnotanamespace}}; 最佳答案 usingbase::