crtp

C++14 Metaprogramming: Automagically build a list of types at compile / init time

不问归期 提交于 2019-12-07 04:11:40
问题 Using C++14 and some combination of the Curiously Recurring Template Pattern (CRTP) and possibly Boost.Hana (or boost::mpl if you wish), can I build a list of types at compile time (or static initialization time) without an explicit declaration? As an example, I have something like this (see it on Coliru ): #include <iostream> #include <boost/hana/tuple.hpp> #include <boost/hana/for_each.hpp> namespace { struct D1 { static constexpr auto val = 10; }; struct D2 { static constexpr auto val = 20

How to fix a purported lack of an “explicit instantiation declaration” when compiling a CRTP Singleton with Clang?

一曲冷凌霜 提交于 2019-12-07 03:34:12
问题 We're using the curiously recurring template pattern to implement singletons. However, with recent Clang versions we're getting a -Wundefined-var-template warning. The suggested fix to which is to add an "explicit instantiation declaration". I attempted to do this, but then I get errors about "explicit specialization after instantiation" in the compilation unit where the definition of the singleton template class member variable is. What's the appropriate construct to fix the issue

Curiously Recurring Template Pattern and statics in the base class

大憨熊 提交于 2019-12-06 20:46:35
So thanks to this answer I'm looking at implementing my problem with CRTP. However I have a problem. In my static base class I have 2 sets of functions. One takes std::vectors and one takes a standard C-style array. So in the base class I define a static function that calls the non-std::vector function. However when I derive from that base class I seem to no longer be able to access the public static function in the base class (Which I thought I could). template< class Derived > class Base { public: static void Func( std::vector< float >& buffer ) { Func( &buffer.front(), buffer.size() ); }

Overload operator in template base class

吃可爱长大的小学妹 提交于 2019-12-06 15:20:10
I've got the following code: #include <iostream> #include <cstring> #define THIS(X) static_cast<X*>(this) template<typename T> class Base { public: Base() { } virtual ~Base() { } void foo() { THIS(T)->a[0] = 1; } T& operator=(const T& o) { std::cout <<"operator="<<std::endl; memcpy(THIS(T)->a, o.a, THIS(T)->size); return static_cast<T&>(*this); } }; template<typename T> class Der1: public Base<Der1<T> > { private: T* a; unsigned int size; public: Der1(int d) { a = new T[d]; size = d; } virtual ~Der1() { delete[] a; } using Base<Der1<T> >::operator=; friend class Base<Der1<T> > ; }; template

how to cast to CRTP in java?

独自空忆成欢 提交于 2019-12-06 12:25:55
I have a pretty simple case where I do some basic generic assignment: final Detail detail = field.getAnnotation(Detail.class); final String example = detail.example(); final Class<?> type = field.getType(); if (List.class.isAssignableFrom(type)) ... else if (Enum.class.isAssignableFrom(type)) setValue(contract, field, Enum.valueOf(type, example)); else if (...) ..... but the Enum.valueOf() is a bit difficult to call, in my case, the error is: valueOf(java.lang.Class,java.lang.String) in java.lang.Enum cannot be applied to (java.lang.Class,java.lang.String) This makes perfectly sense since type

CRTP Dispatch in C++11

隐身守侯 提交于 2019-12-06 05:42:49
Say I have the following code: template <class Derived> class Base { public: virtual void foo_impl() = 0; void foo() { static_cast<Derived*>(this)->foo_impl(); //A (*static_cast<Derived*>(this)).foo_impl(); //B } }; class Derived : public Base<Derived> { private: void foo_impl() { bar(); } }; A few questions: Will line A generate a virtual function call? Although the majority of what I can find on the internet recommends doing things this way, to me I don't see how the compiler can do static dispatch considering that a pointer to Derived could still actually point to an object of type Derived2

Using CRTP with virtual inheritance

依然范特西╮ 提交于 2019-12-06 04:53:09
问题 I have a hierarchy of nodes, where "diamond" can occurred. Every node must be clonable but I don't want to write clone method to every node. So I use CRTP. class Node { public: Node(){} Node(Fill*) { } virtual ~Node() {} virtual Node * clone() const = 0; virtual void id() { std::cout << "Node\n"; } }; //==================================================================== template <typename Base, typename Derived> class NodeWrap : public Base { public: NodeWrap() { } NodeWrap(Fill * arg1) :

Curiously Recurring Template and Template parameter dependent subclassing issues

好久不见. 提交于 2019-12-06 04:10:21
问题 I am trying to make the following code work template < class __derived, class __object = typename __derived::Object > struct Base { using Derived = __derived; using Object = __object; void function(Object o) { return Derived::function(s); } } //template < class __derived > //struct Base { // using Derived = __derived; // using Object = typename Derived::Object; // void function(Object o) { return Derived::function(s); } //} template < class __object > struct Derived : public Base< Derived< _

Dealing with protected/private constructor/destructor for a CRTP design?

守給你的承諾、 提交于 2019-12-05 22:38:16
Consider the following code: #include <iostream> #include <type_traits> // Abstract base class template<class Crtp> class Base { // Lifecycle public: // MARKER 1 Base(const int x) : _x(x) {} protected: // MARKER 2 ~Base() {} // Functions public: int get() {return _x;} Crtp& set(const int x) {_x = x; return static_cast<Crtp&>(*this);} // Data members protected: int _x; }; // Derived class class Derived : public Base<Derived> { // Lifecycle public: Derived(const int x) : Base<Derived>(x) {} ~Derived() {} }; // Main int main() { Derived d(5); std::cout<<d.set(42).get()<<std::endl; return 0; } If

Using declaration for type-dependent template name

丶灬走出姿态 提交于 2019-12-05 12:45:43
问题 When CRTP is used inside a template, (or generally when a template parameter is passed as a base class template argument), is it impossible to name the base's member templates in a using declaration? template< typename d > struct base { template< typename > struct ct {}; template< typename > void ft() {} }; template< typename x > struct derived : base< derived< x > > { using derived::base::template ct; // doesn't work using derived::base::ft; // works but can't be used in a template-id }; It