Forward declaration of class used in template function is not compiled by clang++

前端 未结 4 913
半阙折子戏
半阙折子戏 2021-02-13 22:53

There is this code:

class A;

template 
void fun() {
   A a;
}

class A { 
public: 
   A() {  } 
};

int main() { 
   fun(); 
   return         


        
4条回答
  •  野性不改
    2021-02-13 23:15

    clang++ is using the correct behavior, this is described in section 4.6/9 of the standard (n1905).


    Templates 14.6/9 Name resolution

    If a name does not depend on a template-parameter (as defined in 14.6.2), a declaration (or set of declarations) for that name shall be in scope at the point where the name appears in the template definition; the name is bound to the declaration (or declarations) found at that point and this binding is not affected by declarations that are visible at the point of instantiation.


    To put things in simpler terms; if the name is not dependent on a template-parameter it should be in scope where the definition is found; therefore you'll need to define A before your definition of template void fun ().

提交回复
热议问题