crtp

CRTP Dispatch in C++11

烈酒焚心 提交于 2019-12-22 19:04:07
问题 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

Can a Delphi generic class descend from its class argument?

房东的猫 提交于 2019-12-22 08:20:15
问题 I've been trying to define a generic, inheritable TSingleton class. Here's what I had in progress: TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom) strict private class var FInstance : RealClass; protected procedure InstanceInitialization;virtual; public destructor Destroy; override; class procedure Create; reintroduce; class function Instance : RealClass; class procedure InstanceFree; end; The goal was to be able to "insert" the singleton pattern in an inheritance tree. so

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

我只是一个虾纸丫 提交于 2019-12-22 06:15:10
问题 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?

Why does this code give the error, “template specialization requires 'template<>'”?

人盡茶涼 提交于 2019-12-22 04:39:25
问题 When I try to compile this with Clang template<class T> struct Field { char const *name; Field(char const *name) : name(name) { } }; template<class Derived> class CRTP { static Field<Derived> const _field; }; class Class : public CRTP<Class> { }; Field<Class> const CRTP<Class>::_field("blah"); int main() { } I get error: template specialization requires 'template<>' Field<Class> const CRTP<Class>::_field("blah"); ~~~~~~~~~~~ ^ I don't understand the error at all. What is wrong with my

CRTP — accessing incomplete type members

陌路散爱 提交于 2019-12-21 17:19:58
问题 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;

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

三世轮回 提交于 2019-12-21 15:22:37
问题 This question already has answers here : C++ static polymorphism (CRTP) and using typedefs from derived classes (5 answers) Closed 6 years ago . 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

Curiously recurring template pattern (CRTP) with static constexpr in Clang

做~自己de王妃 提交于 2019-12-21 04:21:14
问题 Consider my simple example below: #include <iostream> template <typename T> class Base { public: static constexpr int y = T::x; }; class Derived : public Base<Derived> { public: static constexpr int x = 5; }; int main() { std::cout << Derived::y << std::endl; } In g++, this compiles fine and prints 5 as expected. In Clang, however, it fails to compile with the error no member named 'x' in 'Derived' . As far as I can tell this is correct code. Is there something wrong with what I am doing, and

How to write a good curiously recurring template pattern (CRTP) in C#

 ̄綄美尐妖づ 提交于 2019-12-21 04:01:20
问题 A while back I wanted to create my own data mapper that would be much simpler than your average ORM. In doing so I found the need to have access to the type information of inheriting classes in my base class. My first thought was reflection, but it's too slow (if you use reflection though, check out Fasterflect as it 'almost' eliminates the performance problems of reflection). So I turned to a solution that I later found out had it's own name: The Curiously Recurring Template Pattern. This

CRTP and c++1y return type deduction

女生的网名这么多〃 提交于 2019-12-21 03:31:07
问题 I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template<typename Derived> struct Base { auto foo() { return static_cast<Derived*>(this)->foo_impl(); } }; struct Derived: public Base<Derived> { auto foo_impl() -> int { return 0; } }; int main() { Derived b; int i = b.foo(); (void)i; } I assumed that the return type from Base<Derived>::foo was a decltype of the expression returned,

Use Curiously Recurring Template Pattern (CRTP) with additional type parameters

只谈情不闲聊 提交于 2019-12-20 12:41:11
问题 I try to use the Curiously Recurring Template Pattern (CRTP) and provide additional type parameters: template <typename Subclass, typename Int, typename Float> class Base { Int *i; Float *f; }; ... class A : public Base<A, double, int> { }; This is probably a bug, the more appropriate superclass would be Base<A, double, int> -- although this argument order mismatch is not so obvious to spot. This bug would be easier to see if I could use name the meaning of the parameters in a typedef: