我看到它的用法如下
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");
};
我不知道它有什么用?
什么是DependentFalse结构?
std::false_type
用作类型特征的构建块,定义为std::integral_constant<bool, false>
(我将在这里跳过)。它的定义可以归结为这样的(简化的):
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
};
它用来表示values false
and true
as types。这在类型特征中非常有用,您可以让类模板继承自其中之一std::false_type
or std::true_type
对于不同的(部分)专业化,取决于模板参数满足的某些条件。这样做可以测试给定类型是否满足类型特征的条件并获得编译时常量value通过访问静态指示结果value
继承自任一成员的成员std::false_type
or std::true_type
或者通过使用转换运算符转换类型特征的实例来替代。
您在这里展示的是一个简单的类型特征,它总是(对于所有T
) 评估为std::false_type
。它用于static_asserts
当它们所在的模板被实例化时,这应该总是失败。这是必要的,因为一个static_assert
不依赖于模板参数的函数已经在定义点而不是实例化点触发,因此使每个程序都包含类似的内容static_assert(false);
格式不正确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)