better-cpp-syntax
better-cpp-syntax copied to clipboard
"template" keyword not highlighted when used as disambiguator
In C++, the template
keyword can appear as a disambiguator before a name in a nested-name-specifier to indicate that the identifier that follows names a template. (This is required for correctly identifying a <
token following the name as being part of a template-id rather than a less-than operator, in cases where the type to the left of the ::
is dependent.)
For example:
struct A {
template <typename T>
struct Nested {};
};
template <typename T>
struct B : T::template Nested<int> {}; // <--
B<A> b;
However, the use of the template
keyword in this context is not highlighted as a keyword.
I'm looking into this problem and its actually being decently hard to track down. A fix will eventually get made though
@jeff-hykin This should be needed only in dependant contexts; therefore, when template (and typename) is used to disambiguate a dependant context, it will always be preceded by ::
.
One the other hand, a template declaration can never be preceded by ::
.
Short of very odd formatting, it should be possible to distinguish a disambiguator (which is really a template call) and a template declaration.
A::a_really_long_dependant_context::
template<a_really_long_type>;
Short of very odd formatting, it should be possible to distinguish a disambiguator (which is really a template call) and a template declaration.
A::a_really_long_dependant_context:: template<a_really_long_type>;
This sort of formatting does occur in the wild (example).
Perhaps a more fruitful heuristic is to check the type of the token that follows the template
: in a template-declarataon, it's a <
token, but after a disambiguator it's an identifier.
Yep, that's a much better check.
So template
is a disambiguator if it preceded by ::
or the next non-whitespace character is not <
.
Confusing that would require template
to be the only thing on the line, and I can't imagine that is very popular.
T:a_dependant_context::
template /* some comment explaining something */
templated_type_name<typename T::value_type>