草庐IT

TEMPLATE

全部标签

c++ - 模板相关的名称解析不应该找到没有链接的声明?

在c++标准[temp.point]中是这样写的:Theinstantiationcontextofanexpressionthatdependsonthetemplateargumentsisthesetofdeclarationswithexternallinkagedeclaredpriortothepointofinstantiationofthetemplatespecializationinthesametranslationunit.然后在[temp.dep.candidate]中:Forthepartofthelookupusingassociatednamespace

c++ - CRTP和template模板参数限制

我正在尝试使用CRTP,但我对以下代码无法编译的原因感到困惑。templateclassCBase>structComponentX:publicCBase{//ThisdoesNOTcompile};templateclassCBase>structComponentY:publicCBase{//Thisdoescompile};你知道在CRTP的情况下模板模板参数是否有一些限制吗? 最佳答案 类模板名称代表“当前特化”(即它是一个注入(inject)的类名)仅在打开{之后类模板定义,在其范围内。在此之前,它是一个模板名称。所以

c++ - 如果放置在该类模板的定义之后,则类模板的默认模板参数无效

请考虑以下示例://MindthedefaulttemplateargumenttemplatestructTest;templatestructTest{};templatestructTest;intmain(){Testt;return0;}上面的代码可以被MSVC19、gcc8和clang8成功编译,符合预期。现在让我们将默认模板参数移动到类模板的定义中:templatestructTest;//MindthedefaulttemplateargumenttemplatestructTest{};templatestructTest;intmain(){Testt;return

c++ - 我可以在不编写自定义特征类的情况下对类型进行模式匹配吗?

由于C++20概念尚未标准化,我使用static_assert作为临时概念检查,以便在不满足类型要求时提供有用的错误消息。在这种特殊情况下,我有一个函数要求在获取结果类型之前可以调用类型:templatevoidexample(){static_assert(std::is_invocable_v,"Functionmustbecallable");usingR=std::invoke_result_t;//...}另外,我要求可调用的结果必须是某种std::optional,但我不知道可选的类型是什么,所以我需要从它:usingR=//...usingT=typenameR::val

c++ - 运算符 () 重载模板 C++

我有一个简单的类,我想如下重载运算符classMyClass{public:intfirst;templateToperator()()const{returnfirst;}};还有我的其他地方MyClassobj;inti=obj();//Thisgivesmeanerrorsayingcouldnotdeduce//templateargumentforT谁能帮我解决这个错误,非常感谢。谢谢。编辑:这与operator()有关,例如,如果我将函数替换为templateTget()const{returnfirst;}它有效。感谢所有回复。 最佳答案

c++ - 模板类的模板特化

我想特化以下成员函数:classfoo{templateTget()const;};其他类bar也依赖于模板。例如,我希望bar是带有一些模板参数的std::pair,类似这样:templatestd::pairfoo::get()const{T1x=...;T2y=...;returnstd::pair(x,y);}其中T1和T2也是模板。如何才能做到这一点?据我所知应该是可能。所以现在我可以调用:some_foo.get>();完整/最终答案:templatestructtraits;classfoo{templateTget()const{returntraits::get(*t

c++ - 模板模板参数和 clang

我在使用模板模板参数和clang时遇到了问题(可能是我的问题)。以下玩具示例在g++4.7.0下编译和运行,而不是clang++3.0(基于LLVM3.0),两者都是ubuntu12.04。玩具示例(test_1.cpp):#include#includestructAFn{voidoperator()(){;//dosomething}};templatestructimpl{T*backpointer_;};templateclassT>structimplT{T*backpointer_;};templateclassAClass;templatestructimplT{impl

c++ - 错误 C2823 : a typedef template is illegal - function pointer

我想使用模板定义一个函数指针类型。但是,VS2013我认为“typedef模板是非法的”。我想写这样的东西:templatetypedefvoid(*FuncPtr)(void*object,SD*data);不幸的是,这无法编译。我想保持简短。基本上我需要为一个函数指针定义一个类型,它的参数是一个模板类。 最佳答案 自C++11起,您可以使用using关键字的效果非常像typedef,它允许模板:templateusingFuncPtr=void(*)(void*,SD*);在此之前,您必须将模板与typedef分开:templa

具有默认参数的 C++ 模板参数

我有一个类需要使用某种map。默认情况下,我想使用std::map,但我也想让用户能够根据需要使用不同的东西(例如std::unordered_map或甚至可能是用户创建的)。所以我的代码看起来像#includetemplateclassMap=std::map>classMyClass{};intmain(){MyClassmc;}但是,g++提示test.cpp:3:61:error:templatetemplateargumenthasdifferenttemplateparametersthanitscorrespondingtemplatetemplateparametert

c++ - Variadic 可变模板模板参数

有没有一种简单的方法来获得可变可变模板模板参数。例如考虑以下函数签名templateclassPack,typenameT,size_t...Args>voidfoo(constPack&a);如果我们想传递两个Pack,我们现在必须做一个重载templateclassPack,typenameT,size_t...Args0,size_t...Args1>voidfoo(constPack&a,constPack&b);现在,如果我们想传递可变数量的Pack对象并使用不同的可变参数,该怎么办?Args0...,Args1...,Args2...。所以我在想是否有一种实用的方法可以按照