c++ - 模板的非静态成员可以专用于数据或函数吗?

标签 c++ templates language-lawyer

GCC、Clang、ICC 和 MSVC 都拒绝这种代码,但我在 C++ 标准的最新工作草案中没有发现任何违反规则的地方。

规则是否已经在标准中,或者在缺陷报告中?

#include <type_traits>

template< typename t >
struct s {
    std::conditional_t< std::is_integral< t >::value, t, void() > mem;
};

s< int > a;
s< void * > b;

最佳答案

由于 14.3.1/3,代码无效:

If a declaration acquires a function type through a type dependent on a template-parameter and this causes a declaration that does not use the syntactic form of a function declarator to have function type, the program is ill-formed.

这里声明的类型依赖于模板参数t,因此不能是函数类型。

关于c++ - 模板的非静态成员可以专用于数据或函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112567/

相关文章:

c++ - C++中可以不用纯虚函数实现抽象类吗?

c++ - C++ 中强制类型推导的示例

c++ - 外联可变参数模板函数

c - 应该返回 -1 的 sprintf 编码错误是什么?

c++ - 为不存在的 map 返回什么迭代器如何签名?

java - 如何连接到 802.15.4 帧流?

c++ - 模板类默认float参数

c++ - 在模板类内 : Use a class method as a template argument for another class method

c - SSE2 有符号整数溢出未定义吗?

c++ - 局部自动函数变量的销毁与返回值的构造之间的顺序