Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const dependent names returned from template functions, where does const go?

Suppose that I have a template function (e.g., foo), that returns a const dependent type. The options to qualify the return type as const is to either put const at the left of typename keyword:

template<typename T>
const typename T::bar
^^^^^
foo(T const& baz) {
  ...
}

or at the right of the dependent type:

template<typename T>
typename T::bar const
                ^^^^^
foo(T const& baz) {
  ...
}

But what if I put the const qualifier between the typename keyword and the dependent type?

template<typename T>
typename const T::bar
         ^^^^^
foo(T const& baz) {
  ...
}

The above as expected, fails to compile for GCC and CLANG, but to my surprise VC++ compiles it fine.

Q:

  • Is this a VC++ extension?
  • Does the C++ standard say anything about where is the appropriate place to put const qualifier in such a context?
like image 554
101010 Avatar asked May 08 '16 19:05

101010


1 Answers

Does the C++ standard says anything about where is the appropriate place to put const qualifier in such a context?

Yes. typename appears in a typename-specifier, whose production is

typename nested-name-specifier identifier
typename nested-name-specifier templateoptsimple-template-id

In other words, it must be followed directly by a nested-name-specifier. const is not allowed.

like image 115
T.C. Avatar answered Sep 17 '22 12:09

T.C.