Limitations of metaprogramming inheritance

I am using template metaprogramming in C ++ to generate a class hierarchy with a list of types like this:

//! Basic typelist class
template<class H, class T>
class Typelist
{
public:
    typedef H Head;
    typedef T Tail;
};

//! Terminating type
class NullType {};

//! Forward declaration
template<class Types>
class Recur;

//! Specialisation to terminate the recursion
template<>
class Recur<NullType>
{
};

//! General recursive class 
template<class Types>
class Recur: public Recur<typename Types::Tail>
{
};

// Dummy classes 
class Type1{};
class Type2{};
class Type3{};
int main()
{
    // Defines the typelist
    typedef Typelist<Type1,Typelist<Type2,Typelist<Type3,NullType>>> Types;

    // Instantiate the recursion
    Recur<Types> recur;
    return 1;
}

      

This will create a class hierarchy:

Recur<Typelist<Type2,Typelist<Type3,NullType>>>

which comes from: Recur<Typelist<Type3,NullType>>

which comes from:

Recur<NullType> (base class)

      

Question: Is there any Visual Studio 2010 compiler that limits the depth of class derivation using this recursive technique? In other words, if my list of types contained N types, would the code above compile and build N classes, even if N is 100,000?

+3


source to share


1 answer


Appendix B of the Standard only defines the minimum level of direct and indirect base classes and nested template instances, and it is an implementation quality issue whether a particular compiler is higher than that. Consult your specific compilers for precision data numbers documentation. Below are the minimum quantities required by the Standard.

Appendix B (informative) Scope of implementation [implimits]



1 Because computers are finite, C ++ implementations are inevitably limited in the size of the programs they can successfully process. Each implementation must document the constraints that are known. This documentation can indicate fixed limits where they exist, such as how to calculate variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

2 Limits may limit quantities that include those described below or others. The number in square brackets following each value is equally recommended as the minimum for that quantity. However, these quantities are guidelines only and do not determine compliance.

- Direct and indirect base classes [16 384].

- Direct base classes for one class [1,024].

- Recursively nested template instances, including replacement on template argument deduction (14.8.2) [1,024].

+2


source







All Articles