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

前端 未结 4 907
半阙折子戏
半阙折子戏 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:18

    Which compiler is right then according to C++ standard?

    Both are correct. This is an ill-formed program. Emphasis mine:

    N3290 14.6¶9
    If a type used in a non-dependent name is incomplete at the point at which a template is defined but is complete at the point at which an instantiation is done, and if the completeness of that type affects whether or not the program is well-formed or affects the semantics of the program, the program is ill-formed; no diagnostic is required.

    That clang++ and other compilers do issue a diagnostic here is a nice-to-have added feature, but a diagnosis is not mandatory. That clause "the program is ill-formed; no diagnostic is required" gives a compiler developer free reign to do just about anything in such circumstances and still be compliant.

提交回复
热议问题