Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Template is_const trong C++ STL

std::is_const (header <type_traits>) kiểm tra xem kiểu T có được qualified bằng const hay không (tức là const T).

Cú pháp

template <class T> struct is_const;

Quy tắc

  • is_const<const int>::value == true
  • is_const<int>::value == false
  • is_const<const int*>::value == false (con trỏ đến const int, nhưng con trỏ bản thân không const).
  • is_const<int* const>::value == true (con trỏ const).
  • is_const<const int&>::value == false (reference không thể const-qualified ở cấp độ trên cùng - reference luôn const về bản chất).

Ví dụ 1: Kiểm tra cơ bản

#include <iostream>
#include <type_traits>
using namespace std;

int main() {
    cout << boolalpha;
    cout << "Kiểm tra is_const:\n";
    cout << "int:           " << is_const<int>::value << '\n';           // false
    cout << "const int:     " << is_const<const int>::value << '\n';     // true
    cout << "const int&:    " << is_const<const int&>::value << '\n';    // false (reference)
    cout << "const int*:    " << is_const<const int*>::value << '\n';    // false (pointer to const)
    cout << "int* const:    " << is_const<int* const>::value << '\n';    // true (const pointer)
    cout << "const int* const:" << is_const<const int* const>::value << '\n'; // true
    return 0;
}

Đầu ra

Kiểm tra is_const:
int:           false
const int:     true
const int&:    false
const int*:    false
int* const:    true
const int* const: true

Ví dụ 2: Các kiểu khác

#include <iostream>
#include <type_traits>
using namespace std;

int main() {
    cout << boolalpha;
    cout << "float:          " << is_const<float>::value << '\n';          // false
    cout << "char:           " << is_const<char>::value << '\n';           // false
    cout << "float*:         " << is_const<float*>::value << '\n';         // false
    cout << "char*:          " << is_const<char*>::value << '\n';          // false
    cout << "const int*:     " << is_const<const int*>::value << '\n';     // false
    cout << "int* const:     " << is_const<int* const>::value << '\n';     // true
    cout << "volatile int:   " << is_const<volatile int>::value << '\n';   // false
    cout << "const volatile int: " << is_const<const volatile int>::value << '\n'; // true
    return 0;
}

Đầu ra

float:          false
char:           false
float*:         false
char*:          false
const int*:     false
int* const:     true
volatile int:   false
const volatile int: true

Ứng dụng

  • Template metaprogramming: Loại bỏ const để lấy kiểu gốc (std::remove_const<T>::type).
  • SFINAE: Overload khác nhau cho const vs non-const.
  • Static assert: Đảm bảo template parameter không bị const-qualified khi không mong muốn.

Mối quan hệ

is_const → Kiểm tra top-level const
remove_const → Bỏ top-level const
add_const → Thêm top-level const

Lưu ý quan trọng

is_const chỉ kiểm tra top-level const (const áp dụng trực tiếp cho T). Không kiểm tra const ở cấp độ sâu hơn (như const int* - con trỏ đến const int).