enable_if std的组合:: Less sizeof ..使MSVC失败
combination of enable_if + std::less + sizeof... makes MSVC fail
这是一个非常简单的代码:
template <typename... Args,
typename std::enable_if<std::less<int>()(sizeof...(Args), 3), int>::type* = nullptr>
void test(std::tuple<Args...>)
{
}
int main()
{
test(std::make_tuple(1, 2));
}
这只是一些enable_if
条件的简单函数模板。(对于进一步的sfinae(。
但它无法编译在 Visual Studio 2019中使用C 17 设置。
error C2672: 'test': no matching overloaded function found
error C2783: 'void test(std::tuple<_Types...>)': could not deduce template argument for '__formal'
但是,我发现它在GCC和Clang 中很好地编译。为什么看似无辜的代码失败了?
有趣的是,如果我将sizeof...(Args)
替换为2
,那么它突然起作用。
edit :我的原始问题不提供enable_if
中的类型,但我发现void*
不允许作为C 17中的非类型模板参数。Matter。,因为即使我更改为std::enable_if<std::less<int>()(sizeof...(Args), 3), int>
,它仍然会失败,但同样的错误。
per [comparisons.less]:
template <class T = void> struct less { constexpr bool operator()(const T& x, const T& y) const; };
constexpr bool operator()(const T& x, const T& y) const;
返回:
x < y
。
操作员是constexpr
。因此,就less
而言,您的代码很好。
但是,从技术上讲,MSVC实际上就在这里&mdash;非型模板参数不得在C 中具有void*
型。MSVC实际上诊断出来。无论如何,这纯粹是巧合。
您可以直接将<
用作解决方法:
template <typename... Args,
typename std::enable_if<(sizeof...(Args) < 3), int>::type = 0>
void test(std::tuple<Args...>)
(请注意,使用int
代替void*
,因此语言pedant绝对没有话要说。(
相关文章:
- 如何使用std::min和std::less返回对象
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 同一对象的"sizeof"的不同答案
- std::less是否应该允许在编译时比较不相关的指针?
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- sizeof(size_t) 可以小于 sizeof(int) 吗?
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 为什么 sizeof(ar)/ sizeof(ar[0]) 在传递给函数时无法在向量上正常工作?
- 无法获得等效的 std::less 来用于嵌套迭代器
- 为什么 MSVC _count_of实现将 0 添加到 sizeof 的结果中?
- alignas() 对 sizeof() 的影响 - 强制性的?
- 在C++中,运算符 sizeof 返回什么数据类型?
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- s.length 和 sizeof(a) 有什么区别?
- sizeof(enum) 可以不同于 sizeof(std::underlying_type<Enum>::
- 如何将数组传递给使用 sizeof 的函数?
- sizeof(size_t) 和 sizeof(ptrdiff_t) 什么时候会有所不同?
- " sizeof "操作员在编程中真的很重要吗,尤其是在构建大型应用程序时?
- enable_if std的组合:: Less sizeof ..使MSVC失败