Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a C++ template class, can I typedef the template parameter using the same name?

If I have a template class:

template<typename Layout>
class LayoutHandler : Handler {
};

and I want to expose the parameter Layout to the user of the class. Then:

template<typename Layout>
class LayoutHandler : Handler {
public:
    typedef Layout Layout; // using the same name
};

VS2012 can compile this code, and give the expected result. (I use std::is_same to check it.) Is this allowed in standard C++03 or C++11?

like image 566
jingyu9575 Avatar asked May 17 '13 16:05

jingyu9575


People also ask

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Which of the following types Cannot be used for a non-type template parameter?

Non-type template parameters are one of the few places in C++ where a visible distinction is made between class types and others: integral types, enum types, pointer types, point- to-member types, reference types and std::nullptr_t can all be used as non-type template parameters, but class types cannot.

Can we pass Nontype parameters to templates?

Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.

Why do we use template template parameter?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.


1 Answers

It is not allowed in C++11.

A typedef is a declaration. (see section 7.1.3)

A template parameter can't be redeclared within its scope (including nested scopes). (see section 14.6.1.6)

C++11 draft standard n3242

like image 84
programmerjake Avatar answered Sep 27 '22 22:09

programmerjake