什么是std :: false_type或std :: true_type? [英] What is std::false_type or std::true_type?
问题描述
我看到它的用法如下
template <typename T>
struct DependentFalse : std::false_type
{};
然后,在这里使用
template <typename T>
class RadarSensor
{
static_assert(DependentFalse<T>::value, "RadarSensor must be created using Identifier template");
};
我不知道它的用途是什么?
I do not have idea what is it used for?
什么是DependentFalse结构?
What is DependentFalse structure?
推荐答案
std :: false_type
用作traits类型的构建块,并定义为 std :: integral_constant< bool,false>
(我将在这里跳过)。它的定义可以归结为以下形式(简化):
std::false_type
is used as a building block in type traits and is defined as std::integral_constant<bool, false>
(which I will skip over here). It's definition boils down to something like this (simplified):
struct false_type {
static constexpr bool value = false;
constexpr operator bool() const noexcept { return value; }
// There is more here, but it doesn't really matter for your question
};
类似地:
struct true_type {
static constexpr bool value = true;
constexpr operator bool() const noexcept { return value; }
// There is more here, but it doesn't really matter for your question
};
用于表示值 false
和 true
作为类型。这在类型特征中很有用,您可以让类模板从 std :: false_type
或 std :: true_type
继承对于不同的(部分)专业化,这取决于template参数满足的某些条件。这样做可以测试给定类型是否满足类型特征的条件,并通过访问静态 value $获得指示时间的编译时间常数 value 。 c $ c>成员,它继承自
std :: false_type
或 std :: true_type
或通过转换
It is used to represent the values false
and true
as types. This is useful in type traits where you let a class template inherit from either std::false_type
or std::true_type
for different (partial) specializations, depending on some condition met by the template argument. Doing so allows one to test whether a given type satisfies the condition of the type trait and to obtain a compile time constant value indicating the result through access to the static value
member which is inherited from either std::false_type
or std::true_type
or alternative through conversion of an instance of the type trait using the conversion operator.
这里显示的是一个简单的类型特征,它始终(对于所有 T
)的值为 std :: false_type
。它在 static_asserts
中使用,当实例化它们所在的模板时,它应该总是失败。这是必要的,因为不依赖模板参数的 static_assert
已经在定义点而不是实例化点被触发,因此使每个程序都包含某些内容。像 static_assert(false);
格式错误。
What you are showing here is a simple type trait which always (for all T
) evaluates to std::false_type
. It is used in static_asserts
that should always fail when the template they are located in is instantiated. This is necessary, because a static_assert
that does not dependent on a template parameter is triggered already at the point of definition, rather than the point of instantiation, therefore making every program containing something like static_assert(false);
ill-formed.
这篇关于什么是std :: false_type或std :: true_type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!