- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想写一个类模板M
接受不完整类型 C
作为模板参数。
但我也想要C
最终定义时具有一些特征。
此代码是否有保证
template <auto> struct Dummy {};
template <typename C>
void check()
{
static_assert(std::is_trivial_v<C>);
}
template <typename C>
struct M : Dummy<&check<C>>
{
//static_assert(std::is_trivial_v<C>);//error: incomplete type
C * p;
};
struct Test;
M<Test> m;
int main()
{
return 0;
}
#if defined(FLAG)
struct Test {};
#else
struct Test { std::string non_trivial_member; };
#endif
最佳答案
From a n4713 ,
实例化点 [temp.point] (17.7.4.1/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. A specialization for a class template has at most one point of instantiation within a translation unit. A specialization for any template may have points of instantiation in multiple translation units. If two different points of instantiation give a template specialization different meanings according to the one-definition rule (6.2), the program is ill-formed, no diagnostic required.
check<Test>
在你的程序中有两个实例化点;一次
M<Test> m;
一次在翻译单元的末尾。
check<Test>
的含义在
M<Test> m
与
check<Test>
的含义不同在翻译单元的末尾。一处,
Test
是不完整的,另一方面它是完整的。正文
check<Test>
肯定有不同的含义。
check
要么立即,要么推迟到以后。您不能依赖于它实际对
check
的主体进行实例化的两个点中的哪一个。 .
关于c++ - "used as non-type template parameter"是否使函数模板隐式实例化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67685121/
我是一名优秀的程序员,十分优秀!