草庐IT

TEMPLATE

全部标签

c++ - 具有两个参数包的函数模板重载决议

考虑以下代码:#includetemplateintf(T...){return1;}templateintf(T...){return2;}intmain(){std::cout它在gcc8.2上编译并打印1,但在clang7上编译失败,因为调用f(1)不明确。如果调用被f()替换,两个编译器都无法编译,声称调用不明确。如果参数包class...T被替换为简单参数classT(并且T...替换为T),两个编译器也声称有歧义。第一个例子中哪个编译器符合标准?我想这归结为函数模板的特定部分排序规则,或者以这种方式使用双参数包是否已经不正确?编辑:我的理解是双包本身不是格式错误的,因为如果

c++ - 为什么只有有效的空可变参数包的模板格式错误?

这是什么道理temp.res#8.3(8)Thevalidityofatemplatemaybecheckedpriortoanyinstantiation.[ Note:Knowingwhichnamesaretypenamesallowsthesyntaxofeverytemplatetobecheckedinthisway.— endnote ]Theprogramisill-formed,nodiagnosticrequired,if:[..](8.3)everyvalidspecializationofavariadictemplaterequiresanemptytempl

c++ - 模板参数推导失败和函数参数/参数不匹配

考虑以下程序:templatestructA{usingX=typenameT::X;};template::X*=nullptr>voidf(T,int);voidf(...);templatevoidg(T,int,typenameA::X*=nullptr);//#voidg(...);intmain(){//f(0,nullptr);//errorg(0,nullptr);//ok}g(0,nullptr)在f(0,nullptr)时编译没有(在Godbolt上的GCC主干和Clang主干下测试)。似乎在#的模板参数推导过程中,编译器不实例化A当它找到参数nullptr时与参数

c++ - 这个模板解析冲突叫什么?

我在使用g++7.3编译这个最小示例时遇到问题templatestructconflict{};templatestructs{intconflict;};templateboolgo(){s*sp;returnsp->conflict实际的错误信息并没有透露什么:||test.cpp:Infunction‘boolgo()’:test.cpp|16col24|error:type/valuemismatchatargument1intemplateparameterlistfor‘templatestructconflict’||returnsp->conflict事实上,编译器试图

c++ - 选择声明模板友元的语法背后的基本原理是什么?

声明模板函数friends涉及一些非常不直观的语法,即使对于C++也是如此!选择额外的语法背后的基本原理是什么?需要吗?使用template不是更有意义吗?关键字?对于那些不知道这一点的人,这里有一个你可能会尝试做的例子:templateclassFoo{intx;friendvoidbar(Foo);};templatevoidbar(Foof){std::cout如果您尝试调用bar(Foo()),您将收到链接器错误。要解决这个问题,你必须转发声明bar(因此也是Foo)然后粘贴一个奇怪的位置在好友声明中。templateclassFoo;templatevoidbar(Foo);

c++ - 模板模板模板参数是扩展还是标准的一部分?

我正在搜索与模板模板参数相关的其他内容,偶然发现了thisanswer它声称标准不允许模板模板参数。但是,以下代码在最新的clang(3.2)和最新的GCC(4.8)中编译:templateclass>classT>structtest{};templateclassT>structfoo{};testbar;这是一个扩展,还是其他答案实际上是错误的并且是标准允许的?如果不是,是否有任何特殊原因遗漏? 最佳答案 在std::vector类模板std::vector传递类型int作为参数。在std::get(some_tuple),函

c++ - 为什么编译器不能推断出自动模板参数,除非我添加 const?

我最近遇到这样的代码问题:constexprautolambda=[]{};templatestructLambda{};templatevoidtest(Lambda){}intmain(){test(Lambda{});}clang和GCC都表明它无法推断l。但是,如果我在那里添加const://----vtemplatevoidtest(Lambda){}然后一切都与clang一起工作。海湾合作委员会仍然失败。这里发生了什么事?它不能推导出const本身吗?这是一个GCC错误,因为它在这两种情况下都不推导l吗? 最佳答案 Is

c++ - 为什么即使直到最后才定义实际类型,依赖名称也可以被认为是完整的

Considerthisexample:templatevoidYeap(T);intmain(){Yeap(0);return0;}templatevoidYeapImpl();structX;templatevoidYeap(T){YeapImpl();//passXtoanothertemplate}templatevoidYeapImpl(){T().foo();}structX{voidfoo(){}};请注意structX直到最后才定义。我曾经认为所有odr使用的名称在实例化时必须是完整的。但是在这里,编译器如何在定义之前将其视为完整类型?我查看了cppreference中

c++ - 为什么 C+ +'s ` 变量模板的行为不符合预期?

#includetemplatestructremove_cvref{usingtype=std::remove_cv_t>;};templateusingremove_cvref_t=typenameremove_cvref::type;templateconstexprboolisCc=std::is_copy_constructible_v>;classAfinal{public:A()=default;template>//errorA(T&&){}};Af(){Aa;returna;}intmain(){}错误信息:error:constexprvariable'isCc'm

c++ - 非命名空间范围内的显式特化在 GCC 中无法编译

以下代码在Clang中编译但在GCC中不编译:templatestructWidget{templatevoidfoo(U){}templatevoidfoo(int*){}};根据C++标准([temp.expl.spec],第2段):Anexplicitspecializationmaybedeclaredinanyscopeinwhichthecorrespondingprimarytemplatemaybedefined这是GCC中的错误吗?如果是,我如何在其错误跟踪器中找到它?这是GCC的输出:prog.cc:13:14:error:explicitspecializatio