std::negation

定义于头文件 <type_traits>
template<class B>
struct negation;
(1) (C++17 起)

构建类型特性 B 的逻辑否定。

类型 std::negation<B> 是拥有基性质(BaseCharacteristic)为 std::bool_constant<!bool(B::value)>一元类型特性 (UnaryTypeTrait) 。

目录

模板参数

B - B::value 可转换到 bool 的任何类型

帮助变量模板

template<class B>
inline constexpr bool negation_v = negation<B>::value;
(C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
B拥有在隐式转换为bool时等于false::value 则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool ,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

可能的实现

template<class B>
struct negation : std::bool_constant<!B::value> { };

示例

#include <iostream>
#include <type_traits>
 
static_assert(
    std::is_same<
        std::bool_constant<false>,
        typename std::negation<std::bool_constant<true>>::type>::value,
    "");
static_assert(
    std::is_same<
        std::bool_constant<true>,
        typename std::negation<std::bool_constant<false>>::type>::value,
    "");
 
int main()
{
    std::cout << std::boolalpha;
    std::cout << std::negation<std::bool_constant<true>>::value << '\n';
    std::cout << std::negation<std::bool_constant<false>>::value << '\n';
}

输出:

false
true

参阅

(C++17)
变参数的逻辑与元函数
(类模板)
(C++17)
变参数的逻辑或元函数
(类模板)
可指定类型的编译期常量
(类模板)

版本历史

  • (当前 | 先前) 2017年3月19日 (日) 06:33Fruderica讨论 | 贡献. . (1,867字节) (+1,867). . (以“{{cpp/title|negation}} {{cpp/types/navbar}} {{dcl begin}} {{dcl header | type_traits}} {{dcl | since=c++17 | num=1 | 1= template<class B> struct negation; }} {{dcl e...”为内容创建页面)