Picaud Vincent
Picaud Vincent

Reputation: 10982

C++17 fold expression syntax?

I am trying to use compact fold expression without success.

For instance here is a working C++17 code

template <bool... B>
struct Fold_And : std::integral_constant<bool, (B && ...)>
{
};

template <bool... B>
constexpr auto Fold_And_v = Fold_And<B...>::value;


template <typename V, typename... Vs>
std::enable_if_t<
    Fold_And_v<std::is_floating_point_v<V>,
               std::is_floating_point_v<Vs>...> >
foo(const V& v, const Vs&...)
{
}

I want to translate it into a more compact form (not using the intermediate Fold_And)

template <typename V, typename... Vs>
std::enable_if_t<std::is_floating_point_v<V> && ... &&
                 std::is_floating_point_v<Vs> >
foo_compact(const V& v, const Vs&...)
{
}

However, this is apparently illegal C++ as both g++ and clang++ compilers fail to compile it.

My question:

Or

Upvotes: 9

Views: 1763

Answers (2)

Jarod42
Jarod42

Reputation: 218098

Fold expression requires parenthesis, so:

(std::is_floating_point_v<V> && ... && std::is_floating_point_v<Vs>)

Upvotes: 6

Rakete1111
Rakete1111

Reputation: 49018

You almost got it! Fold expressions have to be surrounded by parentheses:

template <typename V, typename... Vs>
std::enable_if_t<(std::is_floating_point_v<V> && ... &&
                 std::is_floating_point_v<Vs>)>
foo_compact(const V& v, const Vs&...)
{
}

Notice the parentheses after < and before the last >.

Upvotes: 17

Related Questions