std::add_const, why the implementation works for reference?

Viewed 97

In cppreference add_const it is said we may implement it as:

template< class T> struct add_const { typedef const T type; };

When I try a reference type, like std::add_const<int&>, it is int&.

My question is why it can work without a specific version for reference, since int& const is illegal?

1 Answers

In [dcl.ref]p1 of the C++11 standard, you can find:

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef (7.1.3) or of a template type argument (14.3), in which case the cv-qualifiers are ignored.

Related