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

Suppose I have a template function (e.g. foo ) that returns a dependent type of const . Parameters for getting the return type as const - either put const to the left of the typename keyword:

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

or to the right of the dependent type:

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

But what if I put a 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, does not compile for GCC and CLANG, but, to my surprise, VC ++ compiles it in order.

Q

  • Is it a VC ++ extension?
  • Does the C ++ standard mean anything about where is the right place to place const in this context?
+7
c ++ language-lawyer c ++ 11 templates dependent-name
source share
1 answer

Does the C ++ standard mean anything about where to place the const qualifier in this context?

Yes. typename appears in the typename-specifier, whose setting

typename identifier identifier
typename intested-name-specifier template opt simple-template-id

In other words, it must be executed directly using the nested qualifier name. const not allowed.

+11
source share

All Articles