jingyu9575
jingyu9575

Reputation: 531

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?

Upvotes: 13

Views: 1413

Answers (2)

programmerjake
programmerjake

Reputation: 1824

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

Upvotes: 6

Daemon
Daemon

Reputation: 1675

No till C+11 you can't use it, it gives you an error.

declaration of ‘typedef Layout LayoutHandler::Layout’ error: shadows template parm ‘class Layout’

Upvotes: 1

Related Questions