以下是(不)正确的 C++ 代码吗?为什么?
class MyC;
class MyB {
public:
template <class MyT> static void Gimme() { MyT(); }
MyB() { Gimme<MyC>(); }
} B_;
class MyC {
public: MyC() { }
};
g++ 4.7.2 没有提示。
最佳答案
我猜 C++14 标准的以下部分在这里起作用
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. [...]
(强调我的。)
请注意,与 C++11 相比,这部分文档在 C++14 中发生了显着变化。另见 DR#993 ,这似乎意味着简单地将此类实例化推迟到翻译单元的末尾是一种有效的实现技术。
即使 GCC 在 -std=c++11
模式下接受它,我也会尽可能谨慎地声明您的代码在 C++11 实例化规则下格式错误。
关于c++ - 使用不完整类型的函数模板实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303841/