- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我有一个生成多个 static_assert 的模板类:
template <class T>
class Foo
{
static_assert(!std::is_const<T>::value,"");
static_assert(!std::is_reference<T>::value,"");
static_assert(!std::is_pointer<T>::value,"");
//...<snip>...
}
现在假设我有更多的模板类需要做出相同的断言。
有没有办法使 static_assert
block 可重用?如果您愿意,可以使用“static_assert 函数”。
最佳答案
您可以将所需的特征组合成一个具有描述性名称的特征:
template<typename T> using
is_fancy = ::std::integral_constant
<
bool
, (not std::is_const<T>::value)
and
(not std::is_reference<T>::value)
and
(not std::is_pointer<T>::value)
>;
稍后使用:
static_assert(std::is_fancy<T>::value,"");
关于c++ - 如何使 static_assert block 在模板类中可重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55799464/
我正在尝试创建一个由 std::initializer_list 初始化的示例数组类。我想在编译时检查初始化列表的大小是否不超过数组的大小。由于静态断言只能评估 constexpr,因此不会编译此代码
这是另一个 question 的后续内容 我试图在编译时确定特定实现是否在结构中添加了未命名的填充。像 gcc 这样的特定实现允许使用编译指示来控制结构中的填充和对齐,但代价是与其他实现兼容。由于 C
因此,我正在寻找一种方法,如果用于声明对象的值等于另一个值(不希望使用 C 的 assert 宏),则会导致编译时错误. 是的,我知道为什么会出现这个问题......编译器在他/她提示expressi
是否可以对数组值进行编译时检查? 例子: typedef enum { dummy0 = 0, dummy1, dummy2 } eDummyEnum; typedef str
我的问题是,下面的代码是否有效: template class Class { static_assert(sizeof(i) == 0, "Class instantiated with i
我正在尝试检查参数包中的每个参数是否可以存储在 8 个字节以内(sizeof inline auto invoke(std::uint64_t hash, Arguments... argument
我试图禁止对我已有的类的引用,但我看到了一些奇怪的行为。我已经构建了一个玩具示例来展示正在发生的事情。如果我有这个: template struct something { }; template
我有这样的情况: #define FOO(Readonly) static_assert(Readonly, "Fire!"); Readonly 显然会按字面意思粘贴为“false”或“true”,
我有一个函数模板,它接受一个输出迭代器参数。我如何使用 static_assert 检查实例化是否使用了适当的迭代器? (即,它既是一个输出迭代器,又分配正确类型的元素。) #include #in
在 C++11 中是否有一种优雅的方式来执行条件 static_assert 例如: template class MyClass { COMPILE_TIME_IF( IsTypeBuil
有(不要担心这段代码的长度,专注于struct X和Range)(你可以复制和粘贴并且应该编译): 已编辑 #include #include //This is from file "S
我在编译时过滤一个元组,如果元组为空或者元组包含多个元素,我想输出一条自定义错误消息。 static_assert 似乎有点太复杂了,因为我需要使用 bool 逻辑。 static_assert(si
现代优化 C++ 编译器是否受益于 static_asserts? 例如,如果我断言一个整数只能在一个受限范围内,编译器是通过优化步骤携带该信息还是它仍然只是一个整数? 请不要回答编译器可以考虑它。问
我想做一些static dispatch的工作,让基类static_cast this指针指向派生类并调用同名函数来实现多态。我还想用 static_assert 来确保派生类确实重载了特定功能(否则
我有一个模板函数来操作递归数据结构。在调用的初始函数中,我有以下形式: template auto get(param i) -> int { static_assert(Pos (i.re
我有一个函数模板定义如下: template Test &operator Test &operator::value || std::is_same::valu
我有这段代码来确定模板类型是否有 foo(): template struct has_foo : std::false_type {}; template struct has_foo().fo
我想使用 static_assert 对我的类的配置实现各种限制。早些时候,我只使用一个枚举,并且只允许一个需要所述枚举的构造函数来对我的类实现限制。如果我有类似下面的内容并且范围是从 0 到 4,这
为什么 std 库不使用这些呢?目前,如果对不可复制对象的复制构造函数进行调用,则错误消息可能有点“神秘”或让以前从未遇到过的人感到困惑。 我第一次收到此错误消息时,我不知道问题出在哪里(我从未想过不
为什么主体中的这个 static_assert 比我正在检查的错误早于代码中发生的错误?这是标准行为还是特定于编译器? 我很惊讶地发现这是我想要实现的: template Fizzer_t::Fizz
我是一名优秀的程序员,十分优秀!