gpt4 book ai didi

c++ - 检查保护参数包是否导致程序格式错误?

转载 作者:可可西里 更新时间:2023-11-01 18:29:10 24 4
gpt4 key购买 nike

我不止一次(甚至在 SO 上)看到过这样的代码:

template<typename U, typename... G, typename T = Traits<U>>
struct {
static_assert(sizeof...(G) == 0, "!");
// ...
};

或者这个:

template<typename T, typename... G, typename = std::enable_if_t<condition<T>>
void func(T &&t) {
static_assert(sizeof...(G) == 0, "!");
// ....
}

目的是避免用户通过做这样的事情来破坏游戏规则:

template<typename T, typename = std::enable_if_t<std::is_same<T, int>>
void func(T &&t) {
// ....
}

// ...

func<int&, void>(my_int);

使用保护参数包,默认值不能被覆盖。
另一方面,对大小的检查避免了无用参数对特化的污染。

无论如何,因为[temp.res/8] ,我们有:

The program is ill-formed, no diagnostic required, if:
[...]
- every valid specialization of a variadic template requires an empty template parameter pack, or
[...]

因此,包含上述片段的程序是否格式错误?

最佳答案

“技巧”导致程序格式错误,无需诊断。

标准在您引用的部分中明确说明。

关于c++ - 检查保护参数包是否导致程序格式错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40076078/

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