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.
const
qualifier in such a context?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 identifiertypename
nested-name-specifiertemplate
optsimple-template-id
In other words, it must be followed directly by a nested-name-specifier. const
is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With