<: cannot begin a template argument list

前端 未结 4 531
挽巷
挽巷 2020-12-14 19:07

I get an error <: cannot begin a template argument list on g++ compiler. Code

template class SomeClass;
class Class;

SomeClass<::Cla         


        
相关标签:
4条回答
  • 2020-12-14 19:08

    Put spaces around the < characters:

    SomeClass < ::Class > * cls;
    

    You only actually need to separate < and :, but I like symmetry.

    0 讨论(0)
  • 2020-12-14 19:26

    Try the following instead:

    SomeClass< ::Class>* cls;
    

    You can find more info in this question about digraphs. This question about trigraphs could be helpful also.

    0 讨论(0)
  • 2020-12-14 19:31

    According to the Maximal Munch tokenization principle a valid C++ token must collect/have as many consecutive characters as possible.

    <: is a digraph (an alternative representation of symbol [).

                               Digraph  Equivalent
                                  <:          [
                                  :>          ]
                                  <%          {
                                  %>          }
                                  %:          #
    

    So SomeClass<::Class>* cls; is interpreted as SomeClass[:Class>* cls; which doesn't make any sense.

    Solution: Add a whitespace between < and :

      SomeClass< ::Class>* cls;
                ^
                | 
               White Space
    
    0 讨论(0)
  • 2020-12-14 19:35

    With C++11 the answer to this question changes a bit.

    Pre C++11

    Previous to C++11 the maximal munch rule which is used in lexical analysis to avoid ambiguities and works by taking as many elements as it can to form a valid token caused this:

    <::
    

    to generate the following tokens as:

    <: :
    

    <: is a digraph which translates to [ and so you end up with:

    SomeClass[:Class>* cls;
    

    which is not valid code.

    We can confirm that this is the case by going to the draft C++ standard section 2.4 Preprocessing tokens which says:

    If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail.

    and provides a couple of examples including the following classical maximal munch question:

    [ Example: The program fragment x+++++y is parsed as x ++ ++ + y, which, if x and y are of built-in types, violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct expression. —end example ]

    C++11

    In C++11 this changes, a rule was carved out for this case and the draft C++11 standard added the following:

    Otherwise, if the next three characters are <:: and the subsequent character is neither : nor >, the < is treated as a preprocessor token by itself and not as the first character of the alternative token <:.

    to section 2.5 Preprocessing tokens. So this code will no longer produce and error in C++11.

    This change came from defect report: 1104

    0 讨论(0)
提交回复
热议问题