darune
darune

Reputation: 11000

How to fold and static_assert all parameters?

The following doesn't compile:

  template<typename... Args>
  void check_format(Args&&... args)
  {
      static_assert((true && std::is_fundamental<decltype(args)>::value)...);
  }

Upvotes: 1

Views: 969

Answers (2)

Your attempt looks like a mix between a unary and a binary fold expression. The correct forms of the expression as either unary or binary folds are

static_assert((... && std::is_fundamental<decltype(args)>::value));         // unary
static_assert((true && ... && std::is_fundamental<decltype(args)>::value)); // binary

The unary form works because an empty sequence is implicitly equivalent to true.

By the way, decltype(args) is always gonna be a reference type, either lvalue or rvalue. You probably want to std::remove_reference_t from those types. And you may as well use std::remove_reference_t<Args> for ease of writing.

Upvotes: 4

0x5453
0x5453

Reputation: 13589

This should work:

static_assert((std::is_fundamental_v<Args> && ...));

Longer example on godbolt: https://gcc.godbolt.org/z/9yNf15

#include <type_traits>

template<typename... Args>
constexpr bool check_format(Args&&... args)
{
    return (std::is_fundamental_v<Args> && ...);
}

int main() {
    static_assert(check_format(1, 2, 3));
    static_assert(check_format(nullptr));
    static_assert(!check_format("a"));
    static_assert(check_format());
    struct Foo {};
    static_assert(!check_format(Foo{}));
}

Upvotes: 8

Related Questions