c++ - 如何折叠和static_assert所有参数?
问题描述
以下内容无法编译:
template<typename... Args>
void check_format(Args&&... args)
{
static_assert((true && std::is_fundamental<decltype(args)>::value)...);
}
解决方案
这应该有效:
static_assert((std::is_fundamental_v<Args> && ...));
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{}));
}
推荐阅读
- python - Matplotlib 内存不足
- python - 如何在pyspark中分别旋转多个列
- sql - 日期格式在某些计算机上不起作用,SQL Query
- node.js - Mongodb 4.0 bulkWrite 更新时速度极慢
- vba - 根据单元格中的用户输入自动更改 MS word 表格单元格中的背景颜色
- xpath - Xpath,具有多个类的 div
- eclipse - 通过双击在mac上启动可运行的.jar很热?
- angular - 如何以角度从打字稿控制器设置组件的无效
- algorithm - 有人可以解释一下两个二叉树的合并是如何发生的吗?
- android - MotionLayout:如何限制特定视图上的“OnSwipe”事件而不是整个屏幕上的“OnSwipe”