crtp

CRTP to avoid dynamic polymorphism

五迷三道 提交于 2019-12-17 02:52:36
问题 How can I use CRTP in C++ to avoid the overhead of virtual member functions? 回答1: 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

Eliminate redundancy with CRTP and multiple inheritance

て烟熏妆下的殇ゞ 提交于 2019-12-14 03:57:31
问题 This question is for C++03, not C++11. I have a case where I am using CRTP with multiple inheritance, and I am curious to know if there is a way to remove the redundancy that is created when specifying the type of B below. #include "boost/typeof/typeof.hpp" #include "boost/units/detail/utility.hpp" #include <iostream> #include <string> struct One{}; struct Two{}; template<typename T> struct Type { static std::string name(void) { return boost::units::detail::demangle(typeid(T).name()); } };

CRTP std::is_default_constructible not working as expected

 ̄綄美尐妖づ 提交于 2019-12-14 03:49:57
问题 template <class T> class Base { static_assert(!std::is_default_constructible<T>::value, "T must not be default constructible"); }; struct X1 : Base<X1> {}; struct X2 : Base<X2> { X2() = default; }; struct X3 : Base<X3> { X3() {}; }; struct X4 : Base<X4> { X4() : Base{} {}; }; struct Y1 {}; int main() { // all compile. They shouldn't X1 x1; X2 x2; X3 x3; X4 x4; // all compile. They shouldn't: Base<X1> bx1; Base<X2> bx2; Base<X3> bx3; Base<X4> bx4; Base<Y1> by1; // static assert fires. This is

Ensure that class derived from parent CRTP class implements function

↘锁芯ラ 提交于 2019-12-13 12:55:59
问题 Brief: I want to make sure a derived class implements a member function required by a function within the parent CRTP class. Detail: I have some code like this class Base { public: class Params { public: virtual ~Params() {} }; virtual void myFunc( Params& p ) = 0; }; template< typename T > class CRTP : public Base { public: virtual void myFunc( Base::Params& p ) override { typename T::Params& typedParams = dynamic_cast<typename T::Params&>( p ); static_cast<T*>( this )->myFunc( typeParams );

C++ CRTP and accessing derived's nested typedefs from base

一世执手 提交于 2019-12-13 11:36:17
问题 edit: I'll put a github link here when I am done altering my design for anyone who is interested. Background I'm replacing a boost::intrusive , intrusive_set , with my own implementation as 64-bit compiled intrusive-set stuffs 3 x 8-byte pointers into my container nodes. my container has a limit of 2^16 nodes so I can bring it down to 4-bytes per node with 2x 16-bit offset ordinals (which is a 6x reduction of size). In the example below base is the intrusive-set container. The derived class

How to self register class instances using the CRTP?

我的梦境 提交于 2019-12-13 11:31:30
问题 I have a registry for lambda functions associated with a specific CommandId , where the registered function is supposed to create a concrete instance of a command executor class and supply it with some data: CommandId.h #include <cstdint> enum class CommandId : uint16_t { Command1 , Command2 , }; Registry.h #include <map> #include <functional> #include <string> #include "CommandId.h" class Registry { public: static std::map<CommandId,std::function<void (std::string)>>& GetCommands() { static

In this example why do I need CRTP?

浪尽此生 提交于 2019-12-13 10:26:02
问题 See Object counter example here: Why it just does not inherit from non-template class counter . Why counter should be template? template <typename T> struct counter 回答1: Specializations will give you different counter s for different types. Note that those counters are static data members, thus shared among all instances of the given specialization. 来源: https://stackoverflow.com/questions/37530699/in-this-example-why-do-i-need-crtp

CRTP - Counting Individual Classes and Total Count

谁说我不能喝 提交于 2019-12-13 05:24:42
问题 I've recently learned about CRTP and have been practicing some of the better documented code examples out there to understand it better. Currently the block of code I have is able to count the instances of different derived classes but what if I want to find the total of all instances regardless of class? Here is the code I have: template <typename CountedType> class countable { static unsigned count_; public: countable() { ++count_; } countable(countable const&) { ++count_; } ~countable() {

CRTP: How to infer type of member to be used as return type?

感情迁移 提交于 2019-12-12 18:27:47
问题 I would like to make the return type of a CRTP base method depend on the type of a member in the derived, as for example in: template <typename C> struct sum_a_b { ??? sum() { return static_cast<C*>(this)->a + static_cast<C*>(this)->b; } } template <typename T> struct a_b : sum_a_b<a_b<T>> { T a,b; }; What should I put in place of ??? I tried different ways to declare the return type : template <typename T> struct base { int get_ok() { return static_cast<T*>(this)->value; } auto get_invalid()

Recursively defined nested types (in terms of incomplete types)

风格不统一 提交于 2019-12-12 17:03:25
问题 Where does the recursion in the definition of cycle break ? #include <iostream> using namespace std; template<typename T> struct Recursive { using cycle = struct X : Recursive<X> {}; // would work for Recursive<T> as well }; int main() { Recursive<int> x; return 0; } To my surprise the above code compiles - Is it a valid piece of code and if yes what's the meaning (a brief description) of the type cycle ? 回答1: The struct X : Recursive<X> is an example of the Curiously Recurring Template