在这个27:35的视频中,Bryce Lelbach给出了以下示例:
template<auto... Dims>
struct dimensions {};
struct dynamic_extent {};
constexpr dynamic_extent dyn = {};
dimensions<64, dyn, 32> d;此代码不编译。GCC抱怨说:
<source>:8:27: error: 'struct dynamic_extent' is not a valid type for a template non-type parameter
dimensions<64, dyn, 32> d;
^Clang抱怨:
<source>:8:20: error: a non-type template parameter cannot have type 'dynamic_extent'
dimensions<64, dyn, 32> d;
^
<source>:2:22: note: template parameter is declared here
template<auto... Dims>
^他的例子是简单地错了(这很奇怪,因为他指的是一个使用这个想法的库)还是我没有得到一些东西?
发布于 2018-06-21 18:33:10
这个例子是错误的。这在C++17中不起作用。非类型模板参数必须是以下之一:
std::nullptr_t任意类类型不在此列表中。
请注意,它可以使用枚举作为标记类型:
template<auto... Dims>
struct dimensions {};
enum class dynamic_extent {};
constexpr dynamic_extent dyn = {};
dimensions<64, dyn, 32> d;发布于 2018-06-21 18:32:33
是的,他的榜样是错误的。
非类型模板参数在P0732 (这是C++20特性)之前不能具有类类型.即使在C++20中,这仍然是不正确的,因为为了选择使用dyn作为非类型的模板参数,您需要:
struct dynamic_extent {
auto operator<=>(dynamic_extent ) = default;
};到那时,它就能用了。
我想他的意思是:
dimensions<64, &dyn, 32> d;
// ^^^^传递该指针是很好的--指针在C++17中是可以接受的非类型模板参数(以及更早的方式),只要它们满足其他一些要求--而dyn是这样做的。
https://stackoverflow.com/questions/50975315
复制相似问题