草庐IT

TEMPLATE

全部标签

c++ - 防止非类型模板参数中的类型违规

我通常使用std::size_t模板参数中需要整型常量的地方。但我注意到,类型系统并没有保护我免受那些乐于将负数作为参数传递给这些参数的用户的影响。例如下面的编译给出了灾难性的结果:#includetemplatestructE1{staticvoidapply(){std::coutconstexprTa=T{-1};templatevoidE2(){for(auto&&i:{Is...})std::cout::apply();E2();//std::cout;}Demo有趣的是,变量模板不允许这样做(取消注释main中的最后一行会导致编译错误)。对于struct和function情

c++ - 标签调度、可变参数模板、通用引用和遗漏的 const 说明符

请考虑以下示例(标签分发、可变参数模板、完美转发等,全部合而为一):#include#include#includestructA{};structB{};voiddoIt(A&&,conststd::string&){std::coutvoiddoIt(T&&,Args&&...){std::coutvoidfn(Args&&...args){doIt(T{},std::forward(args)...);}intmain(){conststd::stringfoo="foo";std::stringbar="bar";fn(foo);fn(bar);fn(foo);}在这种情况下,

c++ - 为什么 C++11 中没有模板化的 typedef?

这个问题在这里已经有了答案:WhydoesC++11nothavetemplatetypedef?(1个回答)关闭5年前。为什么委员会决定不批准模板化typedef和模板化using?templateusingmy_vector=std::vector;是合法的。但是templatetypedefstd::vectormy_vector;违法吗?更新。问题WhydoesC++11nothavetemplatetypedef?没有回答。

c++ - 如何检查模板模板类的接口(interface)

我试图根据模板模板参数是否定义了类型type来使用SFINAE重载模板类(例如std::remove_reference有一个type成员类型别名),但我想不出这样做的好方法。比如我想做的templateclassTrait>usingEnableIfHasTypeMember=std::void_t;templateclassTrait,typenameOtherStuff,EnableIfHasTypeMember*=nullptr>classSomething{...}但这给了我一个编译器错误。有什么方法可以检查模板模板参数的接口(interface)吗?

c++ - 可以在模板化的 typedef 上使用模板特化吗?

我想做类似下面的事情(在c++11、c++14中;而不是c++17):templateusingpartner=void;templateusingpartner=X;templateusingpartner=Y;templateusingpartner=Z;但是我得到一个编译错误---error:expectedunqualified-idbefore‘using’---关于第一个模板特化。这样的事情可能吗?(我已经知道我可以使用其中包含using语句的模板化类。我希望直接使用using语句而不使用类包装器,因为它更简单,更多优雅。如果有其他简单、优雅的解决方案,请分享!)

c++ - 这段代码在 C++ 中合法吗

我刚刚发现,当涉及到模板时,这段代码在g++3.4.2中编译并且可以工作,除非不调用m():templateclassC{Te;public:C():e(0){};voidm(){e=0;};};现在可以创建和使用实例了Cc;在c.m()未被调用之前,没有编译错误,但这是合法的吗? 最佳答案 是的,这是合法的。模板规范是,在实例化方法之前,它不存在,因此编译器不会检查它。这是来自thespec的相关内容:14.7.1-Implicitinstantiation-9-Animplementationshallnotimplicitly

c++ - 成员函数不能访问私有(private)成员

我得到了以下代码#include#includetemplateclassdemo{Tdata;public:demo();demo(democonst&k);demo(constT&k);demo&operator=(constdemo&k);templatedemo(constdemo&k);templatedemo&operator=(constdemo&k);~demo();};templatedemo::demo():data(){}templatedemo::demo(democonst&k):data(k.data){}templatedemo::demo(constT&

c++ - 一组类型的模板特化

如何为一组数据类型专门化模板?例如:templateinlineTgetRatio(Tnumer,Tdenom){return(numer/denom);}我想专门针对一组数据类型,因此它只适用于int、long、double和float。因此,如果用户尝试将此函数与char类型一起使用,编译器将抛出错误。 最佳答案 这取决于你想做什么。如果您希望编译器无法为函数调用找到合适的解决方案,您可以使用Flinsch的答案,这可能是最好的,或者您可以使用SFINAE:templateis_an_ok_type:boost::mpl::fa

c++ - 带模板的 typedef

templatestructA{typedefintInt;A::Intb;//Line1(fails)Intc;//Line2(compiles)};intmain(){Ax;x.c=13;}错误error:ISOC++forbidsdeclarationof‘Int’withnotypeerror:extraqualification‘A::’onmember‘Int’error:expected‘;’before‘b’第1行失败但第2行编译。为什么? 最佳答案 你需要一个typenametypenameA::Intb;type

c++ - C++ 中的模板特化

我一直在努力理解模板特化。为什么这会产生错误(实例化后“Tfoo(T,T)[withT=int]”的特化)templateTfoo(Ta,Tb);intmain(){intx=34,y=54;coutTfoo(Ta,Tb){returna+b;}templateintfoo(inta,intb){cout 最佳答案 标准要求在实例化时必须知道所有模板定义,并且每个翻译单元都看到相同的定义。否则你的程序是错误的(事实上不需要诊断)。(所以要解决这个问题,只需将所有模板定义放在程序的顶部即可。)请记住,模板函数不是函数,只是模板。将它们