Compiler error with a fold expression in enable_if_t

╄→гoц情女王★ 提交于 2019-12-11 06:47:31

问题


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;
}

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?


回答1:


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>

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.)



来源:https://stackoverflow.com/questions/56978499/compiler-error-with-a-fold-expression-in-enable-if-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!