jjzjj

c++ - 模板类中没有名为 X 的类模板

当尝试使用GCC4.6.0编译此(类CRTP)代码时:templateclassT>structA;templatestructB:A::templateX>{templatestructX{Umem;};};Ba;我收到错误消息“test.cpp:3:26:错误:‘structB’中没有名为‘X’的类模板”。为什么X在类定义之外似乎是不可见的? 最佳答案 正如EmileCormier正确指出的那样here问题是在A实例化的地方,B仍然是一个不完整的类型,你不能使用内部模板。解决方案是将模板X移到模板B之外。如果它独立于模板B的特定

c++ - CRTP + 特征类 : "no type named..."

我尝试使用模板化类实现CRTP,但在使用以下示例代码时出现错误:#includetemplateclassTraits{public:typedeftypenameT::typetype;//'staticconstunsignedintm_const=T::m_const;staticconstunsignedintn_const=T::n_const;staticconstunsignedintsize_const=T::m_const*T::n_const;};templateclassCrtp{public:typedeftypenameTraits::typecrtp_typ

c++ - 解决 CRTP 函数重载歧义

我有几个函数想用于CRTP基类的派生类。问题是,如果我将派生类传递给用于CRTP类的自由函数,就会出现歧义。一个最小的例子来说明这一点是这个代码:templatestructA{};structC:publicA{};structB{};templatevoidfn(constA&a,constA&b){std::coutvoidfn(constTa,constA&b){std::coutvoidfn(constA&a,constU&b){std::couteverythingworksfineBb;fn(a,a);//failstocompileduetoambiguouscallf

c++ - 为什么 CRTP 实现和接口(interface)方法的命名不同?

无论我在哪里阅读有关CRTP的文章,实际上在我编写的代码中,CTRP类层次结构都类似于以下内容:templateclassBase{public:intfoo_interface(){returnstatic_cast(this)->foo_implementation();}};classDerived:publicBase{friendclassBase;intfoo_implementation(){return5;}};也就是接口(interface)名称和实现方法不同。现在,我通常不希望实现方法从外部可见,这需要上面的friend声明,并且在多级层次结构中证明是一个主要的问题

C++14 元编程 : Automagically build a list of types at compile/init time

使用C++14和CuriouslyRecurringTemplatePattern(CRTP)以及可能的Boost.Hana的某种组合(或boost::mpl如果您愿意),我可以在编译时(或静态初始化时)构建一个类型列表而无需显式声明吗?例如,我有这样的东西(在Coliru上查看):#include#include#includenamespace{structD1{staticconstexprautoval=10;};structD2{staticconstexprautoval=20;};structD3{staticconstexprautoval=30;};}intmain(

c++ - 为什么CRTP不会造成无限嵌套?

我对CRTP的编译方式感到困惑。如果我们有这样的事情:templateclassBase{};classDerived:publicBase{};为什么在编译过程中没有发生类似的事情?(X[Y]表示X继承自Y)根据Derived的声明实例Derivedd;d正在扩展为模板和继承的无限循环d[Base]>]>]>]为什么这不会发生?所有关于CRTP的教程都只解释了你可以用它做什么,而不是(至少是模糊地)解释了幕后发生的事情。 最佳答案 要理解的基本概念是模板的实例只是一个类。它与任何其他类基本上没有什么不同。当你有一个典型的模板定义时

c++ - 如何在 C++ 中强制使用奇怪的重复模板模式

我有以下基本模板类。templateclassBase{public:voiddo_something(){}};它旨在用作奇怪地重复出现的模板模式。它应该像classB:publicBase一样被继承.它必须不像classB:publicBase一样被继承.我想静态地执行这个要求。如果有人使用错误,我预计会在编译阶段出现错误。我正在做的是放置一个static_cast(*this)在do_something().这样,继承模板的类就是或继承自作为模板参数提供的类。对不起,令人困惑的表达。用简单的英语来说,它需要B是或继承自SomeoneElse在classB:publicBase.我

c++ - 为什么此代码会出现错误 "template specialization requires ' template< >'"?

当我尝试用Clang编译它时templatestructField{charconst*name;Field(charconst*name):name(name){}};templateclassCRTP{staticFieldconst_field;};classClass:publicCRTP{};FieldconstCRTP::_field("blah");intmain(){}我明白了error:templatespecializationrequires'template'FieldconstCRTP::_field("blah");~~~~~~~~~~~^我根本不明白这个错

c++ - 派生类作为模板参数有什么用?

这个模式的目的是什么?这叫什么?第一次看到的时候觉得很奇怪,虽然现在已经看到很多次了。templatestructBase{//...};structExample:Base{//...}; 最佳答案 它叫做CuriouslyRecurringTemplate模式,并允许静态多态性。当您想向特定类添加功能,但希望该实用程序在一般情况下可用时,它很有用。通过使实用程序依赖于模板参数并使用模板参数,您可以同时实现这两者。 关于c++-派生类作为模板参数有什么用?,我们在StackOverfl

防止意外隐藏(CRTP Mixin提供的方法)

我对类型对象有许多有用的功能T。这里T需要为功能提供一些接口。界面有几种常见的实现。因此,我让他们使用CRTP作为Mixins工作。templatestructInterfaceImpl{usingImplType=InterfaceImpl;intfoo();...};structMyData:publicInterfaceImpl{...};templatevoidaUsefulFunction(T&t){//Workingwith`t`.//Thiscastistoworkaroundanaccidentalhidingof`foo`byMyData.static_cast(t).foo