crtp

clang++ not accepting use of template template parameter when using CRTP

Deadly 提交于 2019-11-27 22:48:23
I'm getting compilation errors when trying to call the base class constructor in derived initialization list when using a template template parameter with CRTP. Problem can be replicated with this snippet of code: template <template<class> class Derived, class T> struct base { }; template <class T> struct derived : public base<derived, T> { derived() : base<derived, T>() { } }; The offending error messsage: bug.cpp:10:16: error: template argument for template template parameter must be a class template or type alias template : base<derived, T>() ^ bug.cpp:10:11: error: expected class member or

Prevent user from deriving from incorrect CRTP base

五迷三道 提交于 2019-11-27 12:11:13
问题 I cannot think about a proper question title to describe the problem. Hopefully the details below explains my problem clear. Consider the following code #include <iostream> template <typename Derived> class Base { public : void call () { static_cast<Derived *>(this)->call_impl(); } }; class D1 : public Base<D1> { public : void call_impl () { data_ = 100; std::cout << data_ << std::endl; } private : int data_; }; class D2 : public Base<D1> // This is wrong by intension { public : void call

How to avoid errors while using CRTP?

狂风中的少年 提交于 2019-11-27 08:54:33
Using CRTP sometimes I write a code like this: // this was written first struct Foo : Base<Foo, ...> { ... }; // this was copy-pasted from Foo some days later struct Bar : Base<Foo, ...> { ... }; And it's very difficult to understand what goes wrong, until I trace code in debugger and see that Bar's members aren't used in Base . How to reveal this error at compile time? (I use MSVC2010, so I can use some C++0x features and MSVC language extensions) In C++0x you have a simple solution. I don't know whether it is implemented in MSVC10 however. template <typename T> struct base { private: ~base()

invalid use of incomplete type

半城伤御伤魂 提交于 2019-11-27 03:42:37
I'm trying to use a typedef from a subclass in my project, I've isolated my problem in the example below. Does anyone know where I'm going wrong? template<typename Subclass> class A { public: //Why doesn't it like this? void action(typename Subclass::mytype var) { (static_cast<Subclass*>(this))->do_action(var); } }; class B : public A<B> { public: typedef int mytype; B() {} void do_action(mytype var) { // Do stuff } }; int main(int argc, char** argv) { B myInstance; return 0; } This is the output I get: sean@SEAN-PC:~/Documents/LucadeStudios/experiments$ g++ -o test test.cpp test.cpp: In

Force explicit template instantiation with CRTP

纵然是瞬间 提交于 2019-11-27 03:41:41
问题 I am trying to use a CRTPed base to hold some static initialization code like this: template <typename T> class InitCRTP { public: static InitHelper<T> init; }; template <typename T> InitHelper<T> InitCRTP<T>::init; Now, any class which needs to do the work in InitHelper<T> can do this: class WantInit : public InitCRTP<WantInit> { public: void dummy(){init;}//To force instantiation of init }; template class InitCRTP<WantInit>;//Forcing instantiation of init through explicit instantiation of

clang++ not accepting use of template template parameter when using CRTP

时光毁灭记忆、已成空白 提交于 2019-11-26 23:13:29
问题 I'm getting compilation errors when trying to call the base class constructor in derived initialization list when using a template template parameter with CRTP. Problem can be replicated with this snippet of code: template <template<class> class Derived, class T> struct base { }; template <class T> struct derived : public base<derived, T> { derived() : base<derived, T>() { } }; The offending error messsage: bug.cpp:10:16: error: template argument for template template parameter must be a

Creating circular generic references

夙愿已清 提交于 2019-11-26 18:21:47
问题 I am writing an application to do some distributed calculations in a peer to peer network. In defining the network I have two class the P2PNetwork and P2PClient. I want these to be generic and so have the definitions of: P2PNetwork<T extends P2PClient<? extends P2PNetwork<T>>> P2PClient<T extends P2PNetwork<? extends T>> with P2PClient defining a method of setNetwork(T network). What I am hoping to describe with this code is: A P2PNetwork is constituted of clients of a certain type A

How to avoid errors while using CRTP?

大城市里の小女人 提交于 2019-11-26 17:47:35
问题 Using CRTP sometimes I write a code like this: // this was written first struct Foo : Base<Foo, ...> { ... }; // this was copy-pasted from Foo some days later struct Bar : Base<Foo, ...> { ... }; And it's very difficult to understand what goes wrong, until I trace code in debugger and see that Bar's members aren't used in Base . How to reveal this error at compile time? (I use MSVC2010, so I can use some C++0x features and MSVC language extensions) 回答1: In C++0x you have a simple solution. I

CRTP to avoid dynamic polymorphism

China☆狼群 提交于 2019-11-26 14:54:01
How can I use CRTP in C++ to avoid the overhead of virtual member functions? There are two ways. The first one is by specifying the interface statically for the structure of types: template <class Derived> struct base { void foo() { static_cast<Derived *>(this)->foo(); }; }; struct my_type : base<my_type> { void foo(); // required to compile. }; struct your_type : base<your_type> { void foo(); // required to compile. }; The second one is by avoiding the use of the reference-to-base or pointer-to-base idiom and do the wiring at compile-time. Using the above definition, you can have template

Practical Uses for the “Curiously Recurring Template Pattern”

本秂侑毒 提交于 2019-11-26 12:55:08
问题 What are some practical uses for the \"Curiously Recurring Template Pattern\"? The \"counted class\" example commonly shown just isn\'t a convincing example to me. 回答1: Simulated dynamic binding. Avoiding the cost of virtual function calls while retaining some of the hierarchical benefits is an enormous win for the subsystems where it can be done in the project I am currently working on. 回答2: It's also especially useful for mixins (by which I mean classes you inherit from to provide