Confusing Template error

后端 未结 4 1192
萌比男神i
萌比男神i 2020-12-04 11:44

I\'ve been playing with clang a while, and I stumbled upon \"test/SemaTemplate/dependent-template-recover.cpp\" (in the clang distribution) which is supposed to provide hint

4条回答
  •  心在旅途
    2020-12-04 12:18

    Excerpt from C++ Templates

    The .template Construct A very similar problem was discovered after the introduction of typename. Consider the following example using the standard bitset type:

    template 
    void printBitset (std::bitset const& bs) 
    { 
        std::cout << bs.template to_string, 
                                           allocator >(); 
    } 
    

    The strange construct in this example is .template. Without that extra use of template, the compiler does not know that the less-than token (<) that follows is not really "less than" but the beginning of a template argument list. Note that this is a problem only if the construct before the period depends on a template parameter. In our example, the parameter bs depends on the template parameter N.

    In conclusion, the .template notation (and similar notations such as ->template) should be used only inside templates and only if they follow something that depends on a template parameter.

提交回复
热议问题