草庐IT

TEMPLATE

全部标签

c++ - 如何在 C++ 函数之外限制 `using` 语句的范围?

我想定义一些模板特化的静态成员,像这样:namespaceA{templateintC::member1_=5;templateintC::member2_=5;templateintC::member3_=5;templateintC::member1_=6;templateintC::member2_=6;templateintC::member3_=6;...}但为了简化代码(并使其看起来更有条理),我想做这样的事情:namespaceA{{usingT=A1::A2::...::MyClass1;templateintC::member1_=5;templateintC::me

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++ - 使用模板特化

通常的模板结构可以被专门化,例如,templatestructX{};templatestructX{};C++11为我们提供了新的很酷的using语法来表达模板类型定义:templateusingYetAnotherVector=std::vector有没有一种方法可以为这些使用类似于结构模板特化的构造定义模板特化?我尝试了以下方法:templateusingYetAnotherVector=AFancyIntVector;但是它产生了一个编译错误。这有可能吗? 最佳答案 没有。但您可以将别名定义为:templateusingYe

c++ - 表达式 SFINAE 重载传递函数指针的类型

在这个例子中,一个函数被传递给一个隐式实例化的函数模板。//Functionthatwillbepassedasargumentintfoo(){return0;}//Functiontemplatetocallpassedfunctiontemplateintcall(Ff){returnf();}templateintcall(Ff,Aa){returnf(a);}inta=call(foo);我们可以通过为foo()添加重载来破解此代码.intfoo(inti){return0;}名称“foo”现在不明确,示例将不再编译。这可以通过显式提供函数指针类型信息来编译。int(*fun

c++ - 提取类的模板参数并迭代它们的最紧凑的方法是什么?

在下面的小程序中,我展示了我目前用于提取类的模板参数并通过递归辅助函数对其进行迭代的解决方案。我想知道是否有更简洁的方法来做到这一点,正如我在下面评论中的伪代码中所解释的那样。templatestructPack{};templatestructB{staticvoidfoo(){std::coutvoidfoo_helper(Pack&&){B::foo();foo_helper(Pack{});}//terminaterecursionvoidfoo_helper(Pack&&){}structA{typedefPackints;staticvoidfoo(){//thisiswh

c++ - 如何让模板模板参数取一个数值?

我想让模板参数接受具有数字模板参数的模板。这个例子可能过于简化了,但我想要这样的东西:templatestructXX{staticconstintx=X;};templateTX>voidfnx(TXx){static_assert(V==TX::x,"IMPOSSIBLE!");}voidfny(){fnx(XX())}我一定没有理解这个的语法,因为它一定是可能的。我将如何做到这一点? 最佳答案 只是稍微修正一下你的语法——因为模板模板参数指定不当,我们最终会得到这样的东西:templateclassZ,TValue>//^^^

c++ - 如何在 C++ 模板中使用比较表达式?

#includetemplatestd::enable_if_tf(){}//OKtemplatestd::enable_if_t1,int>g(){}//VS2015:errorC2988:unrecognizabletemplatedeclaration/definitionintmain(){}我知道错误是由于编译器将“大于”符号“>”作为模板终止符号。我的问题是:在这种情况下,如何使比较表达式合法? 最佳答案 将表达式放在括号中:#includetemplatestd::enable_if_tf(){}templatestd

c++ - 成员模板变量特化

一个类可以包含一个必须是静态的成员模板变量:classB{public:templatestaticXvar;B(){std::coutvoidPrint(){std::cout它必须作为所有静态成员在类范围之外声明:以下编译并按预期工作:templateTB::var=9;//makesonlysenseforint,float,double...但是如何像下面的非工作代码那样专门化这样的var(gcc6.1的错误消息):templatedoubleB::var=1.123;失败:main.cpp:49:23:error:parseerrorintemplateargumentlis

c++ - 如何确定函数特化的主模板?

函数模板特化的主模板通常是非常直观的,但是,我正在寻找正式规则以了解更令人惊讶的情况。例如:templatevoidf(T,U){}//(1)templatevoidf(T,T){}//(2)templatevoidf(int,int){}//(3);specializes(2),not(1);why?理论上,(3)也可以是(1)的特化,但实验表明它不是。 最佳答案 让我们关注通用模板(1)和(2)的声明。这是两个不同的模板,例如(2)不是(1)的特化。好的,现在我们写一个特化:templatevoidfoo(int,int){}在

c++ - 如何确定将使用哪个模板

我正在学习C++中的模板,我发现了以下示例。据我所知,如果没有匹配的非模板函数,编译器应该总是尝试使用最“专业”的模板,但在这个例子中,第一次调用导致调用函数a(T*)而不是一个(整数*)。为什么?为什么第二次调用的行为不同?templatevoida(T){coutvoida(int*){coutvoida(T*){coutvoidb(T){coutvoidb(T*){coutvoidb(int*){coutTheresultingoutputis:(T*)(int)我希望它是:(int)(int) 最佳答案 仅考虑主模板(因此没