草庐IT

TEMPLATE

全部标签

c++ - 未在可变函数模板中推导出上下文

据我所知,下面的代码应该是“未推导上下文”(或者不是?)templatevoidf(X...args,Yy){}intmain(){f(12);f(1,2,3,4);}但是g++4.9为main中f的两个实例编译它...谁能解释一下? 最佳答案 第一个电话f(12)是病式的。根据[temp.deduct.type]/p5.7,未出现在参数声明末尾的参数包是非推导上下文:Thenon-deducedcontextsare:—[..]—Afunctionparameterpackthatdoesnotoccurattheendofthe

c++ - GCC模板参数模板

我正在尝试使用模板的模板作为参数,当我用clang编译时一切正常,但是当我尝试使用GCC4.8时出现以下错误:can'tdeduceatemplatefor'TReceiver'fromnon-templatetype'DamageAnimationSystem'这是我的情况:我有一个程序,其中类可以为某些类型的事件订阅事件管理器,但不是全部。为此,我继承了一个具有特定类型的类:classDamageAnimationSystem:publicReceiver,publicReceiver在这里,我的类将监听“DamageEvent”和“HealthEvent”,因此我需要为此类事件声

c++ 支持模板元编程中的最后一次调用优化

我正在阅读有关C++模板的内容,并且想对比计算从0到N的总和的函数的两种不同实现方式。不幸的是,我遇到了问题,想通过示例解决几个问题:朴素求和代码:#includetemplatestructSum{//CopiedtheimplementationideafromScottMeyersbook//"EffectiveC++".Isthereabetterway?enum{value=N+Sum::value};};templatestructSum{enum{value=0};};intmain(){//Workswellinthiscase,butgivescompilatione

c++ - 无法在未定义的情况下声明模板变量

我想在头文件中转发声明变量模板,然后在单独的编译单元中进行实际实例化。我被引导相信C++14变量模板的操作与静态类变量非常相似。不幸的是,情况似乎并非如此,它阻止我向前声明我的变量模板。templatestructVariable{staticintvariable;};templateexternintvariable;intmain(){(void)Variable::variable;//(void)variable;//intVariable::variable=42;templateintvariable=23;上面的代码示例在GCC下按原样编译和运行。但是取消注释第10行会

c++ - 将可变参数模板链接在一起

如果将以下代码中的X转换为使用C++11可变参数模板,并且应该支持任意数量的模板参数,它会是什么样子?templatestructA{enumO{offset=OFFSET};enumS{size=2};};templatestructB{enumO{offset=OFFSET};enumS{size=4};};templatestructC{enumO{offset=OFFSET};enumS{size=10};};templateclassB0,templateclassB1,templateclassB2>structX:publicB0,B1::size*B0::offset>

c++ - typedef 和显式实例化之间的代码重复

树.htemplateclassbinary_operation:publicnode{//...unimportantdetails...unsignedevaluate()const;voidprint(std::ostream&os)const;};typedefbinary_operation,'+'>addition;typedefbinary_operation,'*'>multiplication;//...树.cpptemplateunsignedbinary_operation::evaluate()const{//...unimportantdetails...}t

c++ - 模板运算符重载中的类型冲突

很抱歉,这听起来像是一个常见问题,据我所知,我找不到问题的答案。最近的帖子是这个:TemplateSpecializationforbasicPODonly假设我有一个类templateclassA{...};,并且我想将operator+重载为内部二元运算符(两个A类型的对象),以及作为混合二元运算符(A类型的对象和数字POD类型的对象)。理想情况下,我想写的是:#includeusingnamespacestd;//Declare/finetemplatetemplateclassA{...};//InternalbinaryoperatortemplateA::type>oper

c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明

我在嵌套命名空间中有一个模板类的前向声明namespacen1{namespacen2{templatestructA;}usingn2::A;}接着是一个定义,实际上它在不同的文件中,中间有一些东西:structX{};namespacen1{namespacen2{templatestructA{};}usingn2::A;}那么以下总是可以的:n1::n2::Aa;但是这个捷径n1::Aa;在clang中给出编译错误error:toofewtemplateargumentsforclasstemplate'A'除非我删除前向声明;g++两者都接受。clang似乎保留在第一个不包含

c++ - 为什么未调用的模板类成员 *parameters* 被实例化?

这个问题是对立的:Whyuncalledtemplateclassmembersaren'tinstantiated?,作者对某些模板方法未实例化感到惊讶。我遇到了相反的问题:我的部分函数在我不期望的时候被实例化了。采取以下程序:templateclassFoo;templateclassBar{templatevoidBaz(typenameFoo::Xx){}};intmain(){Barbar;}此程序编译失败并出现错误:test.cc:6:40:error:implicitinstantiationofundefinedtemplate'Foo'templatevoidBaz(

C++11 变量参数对齐

这是我要实现的界面:Statementselect("SELECT*FROMPeopleWHEREID>?ANDID“?”在哪里?选择字符串中的在运行时与变量参数列表1462、1477匹配。这是类定义:classStatement{public:Statement(std::stringconst&);templatevoidexecute(Args...param,Actionaction);};不幸的是,这会产生一个错误:test.cpp:133:12:error:nomatchingmemberfunctionforcallto'execute'select.execute(14