반응형

C++17 std::is_base_of_v 사용법 및 예제

  • is_base_of_vC++17부터 도입된 std::is_base_of의 변형으로, std::is_base_of<T, U>::value의 값(즉, true 또는 false)을 나타내는 변수 템플릿입니다.


1. 사용법:

  • cpp

      #include <type_traits>
      #include <iostream>
      
      struct Base {};
      struct Derived : Base {};
      struct Unrelated {};
      
      int main() {
          std::cout << std::boolalpha;
          std::cout << "Base is base of Derived: "   << std::is_base_of_v<Base, Derived>   << '\n';
          std::cout << "Base is base of Unrelated: " << std::is_base_of_v<Base, Unrelated> << '\n';
          return 0;
      }
    


2. 실행 결과:

  • output

      Base is base of Derived: true
      Base is base of Unrelated: false
    

  • 즉, std::is_base_of_v<Base, Derived>std::is_base_of<Base, Derived>::value와 동일하지만, ::value를 생략할 수 있어 코드가 더 간결해집니다.



  • 도움이 되셨으면 하단의 ❤️ 공감 버튼 부탁 드립니다. 감사합니다! 😄
  • 일부 모바일 환경에서는 ❤️ 버튼이 보이지 않습니다.

728x90
반응형

+ Recent posts