crtp

CRTP — accessing incomplete type members

会有一股神秘感。 提交于 2019-12-05 11:46:30
Related questions: one , two After trying to understand CRTP for several days it seems that now I understand even less than before:) Consider the following code: 01 #include <iostream> 02 03 template <class IMPL> 04 class Interace 05 { 06 public: 07 typedef typename IMPL::TYPE TYPE; // ERROR: "...invalid use of incomplete type..." 08 void foo() { IMPL::impl(); } // then why does this work? 09 }; 10 11 class Implementation : public Interface<Implementation> 12 { 13 public: 14 typedef int TYPE; 15 static void impl() { std::cout << "impl() " << std::endl; } 16 }; 17 18 19 int main() 20 { 21

How to count the number of CRTP subclasses of a template class?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 08:27:57
Does anyone know of a method to use CRTP to count the number of subclasses of an object? Suppose we had a setup similar to the following one: template <typename T> class Object { .... }; const unsigned int ObjectSubClassCount = ...; class Subobject : public Object<SubObject> { .... }; class Second : public Object<Second> { .... }; and so on, such that, using TMP, we might have a constant ( ObjectSubClassCount ) that represents the total number of subclasses? Does anyone know a way to do this? Edit: I am wanting to use the result as a template parameter later on, so I need it to be done with

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

自闭症网瘾萝莉.ら 提交于 2019-12-05 08:21:58
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 highlighted by this warning? Simplified Details (much of the logic has been removed, to make a MCVE):

mixing CRTP with SFINAE

不羁岁月 提交于 2019-12-05 07:20:06
问题 I have a base taking derived type as template parameter. The following code works as expected. instantiation of base<non_default_impl> uses non_default_impl::data_t and base<default_impl> throws compilation error because event_data is only a forward declaration. template <typename T> struct event_data; template<typename T> struct tovoid { typedef void type; }; template <typename T, typename enable = void> struct get_data{ typedef event_data<T> type; }; template <typename T> struct get_data<T,

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

主宰稳场 提交于 2019-12-05 06:11:51
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; }; struct D3 { static constexpr auto val = 30; }; } int main() { // How to avoid explicitly defining

How to implement a compile-time check that a downcast is valid in a CRTP?

时光怂恿深爱的人放手 提交于 2019-12-05 04:40:05
I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast bothers me. I need a downcast (not an upcast), so I have to use an explicit cast. In all reasonable cases

Curiously Recurring Template and Template parameter dependent subclassing issues

 ̄綄美尐妖づ 提交于 2019-12-04 09:43:18
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< __Object > > { using Object = __object; void function(Object o) { ... } } And i instantiate an object by

Why can't my Curiously Recurring Template Pattern (CRTP) refer to the derived class's typedefs? [duplicate]

让人想犯罪 __ 提交于 2019-12-04 09:40:41
This question already has an answer here: C++ static polymorphism (CRTP) and using typedefs from derived classes 5 answers When using the curiously recurring template pattern , I am unable to refer to typedefs belonging to the derived class only if I attempt to reference them from the base class; gcc complains no type named 'myType' in class Derived<...> . This seems inconsistent with what is otherwise possible using typedefs, templates, and curiously recurring relationships. Consider: /* crtp.cpp */ #include <iostream> using namespace std; // case 1. simple. class Base { public: typedef int a

Multilevel inheritance in c++ (CRTP)

天涯浪子 提交于 2019-12-04 07:15:18
问题 Please help me solve this problem. WhiteDragon is to call Dragon::attacks() instead of MonsterImplement::attacks() , and there is ambiguity error here. If I change Dragon to be derived from MonsterImplement, then the line std::cout << monster->numAttacks << std::endl; won't compile because Dragon has no numAttacks data member (nor should it, because different types of Dragons are to have different values). So I need WhiteDragon to call Dragon::attacks() and to call finalizeMonster() during

Using declaration for type-dependent template name

丶灬走出姿态 提交于 2019-12-03 23:41:11
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 seems to me that this is a hole in the language, simply because the using-declaration grammar