c++ - 使用不完整类型的函数模板实例化

标签 c++ templates language-lawyer incomplete-type

以下是(不)正确的 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/

相关文章:

兼容类型和参数类型限定符

python - 将变量传递到神社导入或从父 html 文件包含

c++ - C++ struct 静态成员变量可以隐藏非类型模板参数吗?

c++ - 使用自定义顶点标签打印 boost 图

C++ 将指定的 vector 内容复制到另一个 vector

c++ - 模板和循环依赖

c++ - 用于将静态字符串常量与类型相关联的类型特征模式

c++ - 函数返回 void 的无序函数评估

php - linux 正确标志传递 gcc mcrypt.h 位置

C++11 - constexpr 上的 bad_alloc