如何折叠和static_assert所有参数?

dar*_*une 0 c++ static-assert variadic-templates fold-expression c++17

以下不编译:

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

0x5*_*453 5

这应该有效:

static_assert((std::is_fundamental_v<Args> && ...));
Run Code Online (Sandbox Code Playgroud)

关于 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{}));
}
Run Code Online (Sandbox Code Playgroud)