C++ Standard Templates (STL) - Type support (operations on attributes, logical non-functions, std::negation)

type properties

A type attribute defines a compile-time template-based structure for querying or modifying the properties of a type.

Attempting to specialize a template defined in the <type_traits> header causes undefined behavior, except that std::common_type can be specialized as described.

Templates defined in the <type_traits> header file may be instantiated with incomplete types unless otherwise specified, although instantiation of standard library templates with incomplete types is generally prohibited.
 

operations on properties

Logical non-element function

std::negation

template<class B>
struct negation;

(1) (since C++17)

Constructs the logical negation of type attribute B.

The type std::negation<B> is the base characteristic (BaseCharacteristic) of std::bool_constant<!bool(B::value)>The unary type characteristic a> (UnaryTypeTrait) .

Template parameters

B - Any type that makes the expression bool(B::value) a legal constant expression

Auxiliary variable template

template<class B>
inline constexpr bool negation_v = negation<B>::value;

(since C++17)

 Possible implementation

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

Inherited from std::integral_constant

member constants

value

[static]

is true if B has which is equal to false when implicitly converted to bool , false otherwise (Public static member constants)::value

member function

operator bool

Convert the object to bool and return value
(public member function)

operator()

(C++14)

return value
(public member function)

member type

type definition
value_type bool
type std::integral_constant<bool, value>

Call example

#include <iostream>
#include <type_traits>

namespace std
{

template <bool B>
using bool_constant = integral_constant<bool, B>;

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

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::negation<std::bool_constant<true>>::value:       "
              << std::negation<std::bool_constant<true>>::value << std::endl;
    std::cout << "std::negation<std::bool_constant<false>>::value:      "
              << std::negation<std::bool_constant<false>>::value << std::endl;

    return 0;
}

output

std::negation<std::bool_constant<true>>::value:       false
std::negation<std::bool_constant<false>>::value:      true

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/134899100