Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function template instantiation using an incomplete type

Is the following (in)correct c++ code and why?

class MyC;
class MyB {
public:
     template <class MyT> static void Gimme() { MyT(); }
     MyB() { Gimme<MyC>(); }
} B_;

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

g++ 4.7.2 does not complain.

like image 891
blazee Avatar asked Jan 28 '26 10:01

blazee


1 Answers

I'd guess that the following portion of C++14 standard is at work here

14.6.4.1 Point of instantiation

8 A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. [...]

(Emphasis mine.)

Note that this portion of document was changed significantly in C++14 compared to C++11. Also see DR#993, which seems to imply that simply postponing such instantiations to the end of translation unit is a valid implementation technique.

I would go as far as cautiously state that your code is ill-formed under C++11 instantiation rules, even though GCC accepts it in -std=c++11 mode.

like image 81
AnT Avatar answered Jan 30 '26 23:01

AnT