better-cpp-syntax icon indicating copy to clipboard operation
better-cpp-syntax copied to clipboard

"template" keyword not highlighted when used as disambiguator

Open HighCommander4 opened this issue 5 years ago • 5 comments

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.

HighCommander4 avatar Aug 25 '19 16:08 HighCommander4

Screen Shot 2019-08-25 at 12 18 38 PM

jeff-hykin avatar Aug 25 '19 17:08 jeff-hykin

I'm looking into this problem and its actually being decently hard to track down. A fix will eventually get made though

jeff-hykin avatar Sep 11 '19 14:09 jeff-hykin

@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>;

matter123 avatar Nov 22 '19 19:11 matter123

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.

HighCommander4 avatar Nov 22 '19 19:11 HighCommander4

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>

matter123 avatar Nov 22 '19 19:11 matter123