C++ 標準テンプレート (STL) - 型のサポート (型の変更、指定された整数型を符号付きにする、std::make_signed)

タイププロパティ

type 属性は、型のプロパティをクエリまたは変更するためのコンパイル時のテンプレート ベースの構造を定義します。

<type_traits> ヘッダーで定義されたテンプレートを特殊化しようとすると、std::common_type が説明どおりに特殊化できる場合を除き、未定義の動作が発生します。

<type_traits> ヘッダー ファイルで定義されたテンプレートは、特に指定されない限り、不完全な型でインスタンス化できますが、不完全な型による標準ライブラリ テンプレートのインスタンス化は一般に禁止されています。
 

型の変更


型変更テンプレートは、テンプレート パラメーターに変更を適用することにより、新しい型定義を作成します。結果の型には、メンバーの typedef 型を通じてアクセスできます。
 

指定された整数型を符号付きにします

std::make_unsigned

テンプレート< class T >
struct make_signed;

(C++11以降)

T が整数 (bool を除く) または列挙型の場合、T に対応する符号付き整数型のメンバー typedef を指定します。 type 、同じ cv 修飾子を持ちます。

それ以外の場合、動作は未定義です。

メンバータイプ

名前 意味
type T に対応する符号付き整数型

補助タイプ

テンプレート< class T >
using make_signed_t = typename make_signed<T>::type;

(C++14以降)

 可能な実装

  /// make_signed
  template<typename _Tp>
    struct make_signed 
    { typedef typename __make_signed_selector<_Tp>::__type type; };

  // Integral, but don't define.
  template<>
    struct make_signed<bool>;

#if __cplusplus > 201103L
  /// Alias template for make_signed
  template<typename _Tp>
    using make_signed_t = typename make_signed<_Tp>::type;

  /// Alias template for make_unsigned
  template<typename _Tp>
    using make_unsigned_t = typename make_unsigned<_Tp>::type;
#endif

通話例

#include <iostream>
#include <type_traits>

int main()
{
    typedef std::make_signed<char>::type char_type;
    typedef std::make_signed<int>::type int_type;
    typedef std::make_signed<volatile long>::type long_type;
    typedef std::make_signed<long long>::type long_long_type;
    typedef std::make_signed<short>::type short_type;
    typedef std::make_signed<int8_t>::type int8_t_type;
    typedef std::make_signed<int16_t>::type int16_t_type;
    typedef std::make_signed<int32_t>::type int32_t_type;
    typedef std::make_signed<int64_t>::type int64_t_type;

    std::cout << std::boolalpha;
    std::cout << "std::is_same<std::make_signed<char>::type, signed char>::value:   "
              << std::is_same<char_type, signed char>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<int>::type, signed int>::value:    "
              << std::is_same<int_type, signed int>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<volatile long>::type, volatile signed long>::value:   "
              << std::is_same<long_type, volatile signed long>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<long long>::type, signed long long>::value:    "
              << std::is_same<long_long_type, signed long long>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<short>::type, signed short>::value:    "
              << std::is_same<short_type, signed short>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<int8_t_type>::type, int8_t>::value:    "
              << std::is_same<int8_t_type, int8_t>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<int16_t_type>::type, int16_t>::value:    "
              << std::is_same<int16_t_type, int16_t>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<int32_t_type>::type, int32_t>::value:    "
              << std::is_same<int32_t_type, int32_t>::value << std::endl;
    std::cout << "std::is_same<std::make_signed<int64_t_type>::type, int64_t>::value:    "
              << std::is_same<int64_t_type, int64_t>::value << std::endl;

    return 0;
}

出力

std::is_same<std::make_unsigned<char>::type, unsigned char>::value:   true
std::is_same<std::make_unsigned<int>::type, unsigned int>::value:    true
std::is_same<std::make_unsigned<volatile long>::type, volatile unsigned long>::value:   true
std::is_same<std::make_unsigned<long long>::type, unsigned long long>::value:    true
std::is_same<std::make_unsigned<short>::type, unsigned short>::value:    true
std::is_same<std::make_unsigned<int8_t_type>::type, uint8_t>::value:    true
std::is_same<std::make_unsigned<int16_t_type>::type, uint16_t>::value:    true
std::is_same<std::make_unsigned<int32_t_type>::type, uint32_t>::value:    true
std::is_same<std::make_unsigned<int64_t_type>::type, uint64_t>::value:    true

おすすめ

転載: blog.csdn.net/qq_40788199/article/details/134588484