问题
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