草庐IT

Templates

全部标签

c++ - 模板函数定义

我有这个代码:templateautooperator*(constT1&a,constT2&b)->decltype(viennacl::linalg::prod(a,b)){returnviennacl::linalg::prod(a,b);}templateautooperator*(constT1&a,constT2&b)->decltype(prod(a,b)){returnprod(a,b);}viennacl::linalg::prod和prod是两个不同的函数(也可以模板化)。我认为如果某些模板函数定义不能用于某些类型,编译器会默默地忽略它并尝试另一个定义。错了吗?Cl

c++ - 默认模板类参数混淆了 g++?

昨天我遇到了一个g++(3.4.6)编译器问题,我使用Intel(9.0)编译器编译的代码没有问题。这是显示所发生情况的代码片段:templateclassFoo{};structBar{voidmethod(Fooconst&stuff=Foo());};g++编译器错误是:foo.cpp:5:error:expected`,'or`...'before'>'tokenfoo.cpp:5:error:wrongnumberoftemplatearguments(1,shouldbe2)foo.cpp:2:error:providedfor`templatestructFoo'foo.

C++:不能调用没有对象的成员函数

我有一个名为MessageQueue的类,它有2个方法(一个模板,一个普通),签名不同,但名称相同。所有的方法都是静态的。在我的类MessageQueue(MessageQueue.h)中:templatevoidMessageQueue::sendMessage(const___handlequeue,constMESSAGE_ACTION&action,constT*pMessage){sendMessage(queue,action,(void*)pMessage,sizeof(pMessage));}在MessageQueue.cpp中:voidMessageQueue::se

c++ - 为什么允许嵌套类模板的部分特化,而不允许完全特化?

templatestructA{templatestructB{};.templatestructC{};};templatetemplatestructA::B{};//error:enclosingclasstemplatesarenotexplicitlyspecializedtemplatetemplatestructA::C{};//ok那么,如果外部类也不是特化的,那么为什么不允许对内部嵌套类(或函数)进行显式特化呢?很奇怪,如果我只是部分通过简单地添加一个虚拟模板参数来专门化内部类,我就可以解决这个问题。使事情变得更丑陋和更复杂,但它有效。我会将完全特化视为部分特化的子集

c++ - 非命名空间范围内的显式特化

这个问题在这里已经有了答案:C++syntaxforexplicitspecializationofatemplatefunctioninatemplateclass?(9个回答)关闭8年前。templateclassCConstraint{public:CConstraint(){}virtual~CConstraint(){}templatevoidVerify(intposition,intconstraints[]){}templatevoidVerify(int,int[]){}};在g++下编译会出现以下错误:Explicitspecializationinnon-name

c++ - 派生模板类访问基类成员数据

这个问题是对thisthread中提出的问题的进一步插入.使用以下类定义:templateclassFoo{public:Foo(constfoo_arg_tfoo_arg):_foo_arg(foo_arg){/*dosomethingforfoo*/}TFoo_T;//eitheraTypeAoraTypeB-TBDfoo_arg_t_foo_arg;};templateclassBar:publicFoo{public:Bar(constfoo_arg_tbar_arg,consta_arg_ta_arg):Foo(bar_arg)//base-classinitializer{

c++ - 模板成员函数重载问题

N4296::13.1/2.2[over.load]的标准说:Likewise,memberfunctiontemplatedeclarationswiththesamename,thesameparameter-type-list,andthesametemplateparameterlistscannotbeoverloadedifanyofthemisastaticmemberfunctiontemplatedeclaration.所以,我认为下面的程序是病式的:#includestructA{templatestaticvoidfoo(){}templatestaticintf

c++ - 使用可变参数模板的递归继承

考虑以下代码:#includestructActionOption{virtualvoidfoo(int)const=0;};templatestructActionType;templatestructActionType:ActionOption{virtualvoidfoo(int)constoverride{std::cout::foo(int)called.\n";}};templatestructActionType:ActionOption{virtualvoidfoo(int)constoverride{std::cout::foo(int)called.\n";}};

c++ - 配对从可变参数模板中随机获得的兼容类型

ABow只能发射Missile类型Arrow,Bolt,或Dart(但不是Stone),并且只能与MissileContainer一起使用类型Quiver或Case.Quiver只能装箭或bolt,而Case只能装bolt、飞镖或石头。我已经声明了这一点:structBow:MissileFireWeapon,MissileTypes,MissileContainerTypes{/*...*/};structQuiver:MissileContainer,MissileTypes{};structCase:MissileContainer,MissileTypes{};在哪里templ

已编译但未使用的 C++ 模板 - mac OS Lion

我有一个非常非常奇怪的情况。我编写的代码看起来像这样:classFoo{public:templateintdoSomething(){std::cout如果我尝试像这样调用doSomething:std::cout();std::cout输出:PrePost我没有得到HelloWorld的输出,函数中的任何内容都没有执行。这自然意味着我的程序稍后会崩溃,因为我需要执行该函数。但是,如果我进行模板特化:templateintdoSomething(){std::cout那么我的输出是:PreHelloWorldint!Post这不会发生在Windows或Linux上,只会发生在Mac上