최대 1 분 소요

개요

  • 지정된 유형의 정적 상수를 래핑
  • bool의 경우 true_type, false_type로 typedef 되어 있고 C++17에서는 bool_constant로 using 되어 있음
  • 동일 유형에 대해 오버로딩 가능


예제

  • 코드
    #include <iostream>
    #include <type_traits>

    using namespace std;

    void func(true_type) { cout << "true" << endl; }

    void func(false_type) { cout << "false" << endl; }

    int main() {
    	func(bool_constant<true>());
    	func(bool_constant<false>());

    	cout << "------ 1" << endl;

    	cout << integral_constant<int, 2>::value * 2 << endl;

    	return 0;
    }
  • 실행 결과
    true
    false
    ------ 1
    4