草庐IT

TEMPLATE

全部标签

c++ - 为什么我的 Curiously Recurring Template Pattern (CRTP) 不能引用派生类的 typedef?

这个问题在这里已经有了答案:C++staticpolymorphism(CRTP)andusingtypedefsfromderivedclasses(5个答案)关闭9年前。使用curiouslyrecurringtemplatepattern时,如果我试图从基类中引用属于派生类的typedef,则仅无法引用它们;gcc提示notypenamed'myType'inclassDerived.这似乎与使用typedef、模板和奇怪的重复关系的其他方式不一致。考虑:/*crtp.cpp*/#includeusingnamespacestd;//case1.simple.classBase{

c++ - std::get() 如何与 std::tuple 一起工作?

尝试制作std::get(std::tuple)之后我自己的方法,我不太确定它是如何被编译器实现的。我知道std::tuple有一个这样的构造函数:tuple(Args&&...args);但是args...到底是什么?分配给?我认为这对于了解如何使用很有用std::get()有效,因为需要将参数放在某个地方才能访问它们。 最佳答案 这是tuple的粗略玩具实现-喜欢上课。首先,一些元编程样板,用于表示整数序列:templatestructseq{};templatestructmake_seq:make_seq{};templat

C++1y/C++14 : Converting static constexpr array to non-type template parameter pack?

假设我有一个静态存储持续时间的constexpr数组(已知范围):constexprTinput[]=/*...*/;我有一个需要打包的输出类模板:templatestructoutput_template;我想像这样实例化output_template:usingoutput=output_template;一种方法是:templatestructmake_output_template{templatestaticconstexproutput_templatef(std::index_sequence){return{};};usingtype=decltype(f(std::m

c++ - 为什么 C++11 没有模板类型定义?

为什么C++11没有“模板类型定义”,比如templatetypedefstd::vector>vec;相反,他们只允许新语法:templateusingvec=std::vector>; 最佳答案 n1406是HerbSutter提出的“typedef模板”的提议,它模仿了您问题中的语法。n1499建议“模板别名”取代它,其中包含using当前存在于C++11中的语法。“typedef模板”的主要缺点之一在这两篇论文中都得到了解决。来自n1406:Inexistingpractice,includinginthestandardl

c++ - 有没有理由使用 "::template"?

从全局命名空间获取模板名称时,您可以使用template关键字:templatevoidfunction_template();templatevoidh(){::templatefunction_template();}intmain(){h();}但是这段代码可以在没有它的情况下编译。在什么情况下可能需要这样做? 最佳答案 我能想到一个地方,但我认为它不太常见:#include//simpilefunctiontemplatetemplatevoidfunction_template(T){std::cout输出voidfunc

c++ - 专用内部类模板函数的类外定义?

请考虑以下格式错误的程序:structS{templatestructJ{};};templatestructS::J{voidf();};templatevoidS::J::f(){}//ERROR$clang++-std=c++11test.cppnofunctiontemplatematchesfunctiontemplatespecialization'f'$g++-std=c++11test.cpptemplate-id‘f’for‘voidS::J::f()’doesnotmatchanytemplatedeclaration为什么f的定义不能编译?如何在上面正确定义函数f

c++ - 在 C++ 中解压参数包

我有两个函数f和g。f异步计算它的返回值并返回一个future。现在,基于f的几个返回值,我想调用g,但我想确保f的值的计算>并行发生。考虑以下代码:templatestd::futuref(T&t);templatevoidg(T&&...t)templatevoidcall_wrapper(T&&...t){autof1=f(t1);//HowdoIsetthevaluesoff1...fnautof2=f(t2);...g(f1.get(),f2.get()....);//HowdoIcallg}如何从call_wrapper函数的可变参数模板T中解压类型?

c++ - 可变模板构造函数优先级

给定以下简单的structtemplatestructA{A(Ta){}templateA(Ta,Ts...more){}};intmain(){Aa(1);}A(Ta)将被调用而不是可变模板构造函数的保证是什么,为什么? 最佳答案 您要查找的标准部分是§14.8.2.4IfAwastransformedfromafunctionparameterpackandPisnotaparameterpack,typedeductionfails.Otherwise,usingtheresultingtypesPandA,thededuct

c++ - 可变递归模板

我有一种特殊的格式,需要以空格分隔的标记和最后的空终止符(空是输出的一部分)。我创建了一个函数来将一系列以空格分隔的标记发送到输出流://C++variadictemplatefunctiontooutputtokenstoastreamdelimitedbyspacestemplatevoidjoin(std::ostream&os,Tconst&arg){//Thisisthelastargument,soinsertanullinthestreamtodelimitosvoidjoin(std::ostream&os,Tconst&arg,Args...args)//recurs

c++ - 具有不同模板参数的函数返回类型

我有一些C++类都具有相同的模板参数templatestructA{};templatestructB:A{};templatestructC:A{};等等。我还有一系列方法可以用于这些类中的任何一个。但是,问题出在返回类型上。我希望此方法返回传入类的实例,整数减一。例如,如果我只是重载函数,它看起来像这样templateAfunc(constA&a){}templateBfunc(constB&a){}templateCfunc(constC&a){}有没有一种方法可以在不重载每种类型的函数的情况下实现这一点?我的意思是......是否可以用单个模板函数替换它们?所有功能的逻辑都是相