gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:07:09 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com