在enable_if_t中带有折叠表达式的编译器错误 [英] Compiler error with a fold expression in enable_if_t

查看:89
本文介绍了在enable_if_t中带有折叠表达式的编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,其中我使用fold表达式评估所有pack参数是否都可以转换为第一个函数参数。由于某种原因,当我进行看起来很微不足道的更改时,它无法在msvc上编译:

I have the following code, where I am using a fold expression to evaluate whether all pack parameters are convertible to the first function argument. For some reason it fails to compile on msvc when I make what seems like a very trivial change:

#include <type_traits>

#define TRY 1

#if TRY == 1

template<typename B, typename... Args,
std::enable_if_t<((std::is_convertible_v<Args&, B&> && ...)), bool> = true>
void fn(B b, Args...args) {}

#else

template<typename B, typename... Args,
typename = std::enable_if_t<(std::is_convertible_v<Args&, B&> && ...)>>
void fn(B b, Args...args) {}

#endif

int main()
{
    fn(5, 4, 2);
    return 0;
}

TRY 更改为 0 进行编译,演示在: https: //godbolt.org/z/EGvQ-N

Change TRY to 0 to have it compile, demo at: https://godbolt.org/z/EGvQ-N

我缺少的两个变体之间是否存在重要区别,或者这是编译器错误?

Is there an important difference between the two variants that I am missing, or is this a compiler bug?

推荐答案

冒着稍微偏离主题的风险,我不确定折叠表达式是此处的最佳选择。我鼓励您使用MSVS支持的 std :: conjunction 变体:

At the risk of being slightly off topic, I'm not sure a fold expression is the best option here. I encourage you to use the std::conjunction variant, which MSVS supports:

- std::enable_if_t<((std::is_convertible_v<Args&, B&> && ...)), bool> = true>
+ std::enable_if_t<std::conjunction_v<std::is_convertible<Args&, B&>...>, bool> = true>

是的,它比较冗长,但也许更清晰。我按照最初的要求使用@NathanOliver来跟踪潜在的MSVS错误。

True, it's more verbose, but maybe clearer. I defer to @NathanOliver to track down the potential MSVS bug as originally asked.

(本来可以作为注释,但认为代码块更清晰。)

(Would have put this as a comment, but thought the code block was clearer.)

这篇关于在enable_if_t中带有折叠表达式的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆