Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer to a class template, as a template, from within its own class definition?

Tags:

c++

templates

Here's the scenario:

template <template <typename> class T, typename V>
struct parent {
    void do_something();
};

template <typename V>
struct child : public parent<child, V> {
    void do_something(V argument);
    using parent<child, V>::do_something; // C3200: invalid template argument for template parameter 'IMPL', expected a class template
};

The above code fails to compile on the given line with the given error (MSVC 9.0). However if I write this instead, outside of the class definition for child:

template <typename V>
struct parent_identity_meta {
    typedef typename parent<child, V> type; // no error!
};

I can now successfully do the following, within child:

using parent_identity_meta<V>::type::do_something;

I know there's a limitation (alleviated in C++11) that you can't typedef against a template, but I don't think that's what I'm running into here, otherwise the typedef in parent_identity_meta would fail. It seems like child refers to the template when not inside of its own class definition, and to the class being generated from within itself.

This is pretty understandable (having to write child<V> every single time would be painful); but is there any way to override this behaviour?

like image 486
Mark McKenna Avatar asked Oct 10 '12 01:10

Mark McKenna


People also ask

What is template What is the need of template declare a template class?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. To simply put, you can create a single function or single class to work with different data types using templates. C++ template is also known as generic functions or classes which is a very powerful feature in C++.

What is class template and function template in C++?

A template allows us to create a family of classes or family of functions to handle different data types. Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster. Multiple parameters can be used in both class and function template.

How do I create an instance of a template class?

To instantiate a template class explicitly, follow the template keyword by a declaration (not definition) for the class, with the class identifier followed by the template arguments.

What is the difference between class template and function template?

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.


1 Answers

This is a place where C++03 and C++11 are different from each other. The relevant part of the standard is [temp.local]/1. In C++03, this states:

Like normal (non-template) classes, class templates have an injected-class-name (clause 9). The injected-class-name can be used with or without a template-argument-list. When it is used without a template- argument-list, it is equivalent to the injected-class-name followed by the template-parameters of the class template enclosed in <>. When it is used with a template-argument-list, it refers to the specified class template specialization, which could be the current specialization or another specialization.

This means that child (without any template arguments) refers to the specialization child<V>. In C++11, it was changed to:

Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injected- class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

Note in particular When it is used ... as a template-argument for a template template-parameter ... it refers to the class template itself.. This means that in C++11, your code would be correct.

like image 163
Mankarse Avatar answered Oct 15 '22 22:10

Mankarse