gpt4 book ai didi

c++ - 如何使 static_assert block 在模板类中可重用?

转载 作者:太空狗 更新时间:2023-10-29 19:42:58 24 4
gpt4 key购买 nike

假设我有一个生成多个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com