형식 특성에 대한 컴파일러 지원(C++/CLI 및 C++/CX)

Microsoft C++ 컴파일러는 컴파일 시간에 형식의 다양한 특성을 나타내는 형식 특성을 C++/CLI 및 C++/CX 확장에서 지원합니다.

모든 런타임

설명

형식 특성은 라이브러리를 작성하는 프로그래머에게 특히 유용합니다.

다음 목록에는 컴파일러에서 지원하는 형식 특성이 나와 있습니다. 형식 특성의 이름으로 지정된 조건이 충족되지 않으면 모든 형식 특성이 반환 false 됩니다.

(다음 목록에서 코드 예제는 C++/CLI로만 작성됩니다. 그러나 해당 형식 특성은 달리 명시되지 않는 한 C++/CX에서도 지원됩니다. "플랫폼 형식"이라는 용어는 Windows 런타임 형식 또는 공용 언어 런타임 형식을 나타냅니다.)

  • __has_assign(type)

    플랫폼 또는 네이티브 형식에 복사 할당 연산자가 있는지 여부를 반환 true 합니다.

    ref struct R {
    void operator=(R% r) {}
    };
    
    int main() {
    System::Console::WriteLine(__has_assign(R));
    }
    
  • __has_copy(type)

    플랫폼 또는 네이티브 형식에 복사 생성자가 있는지 여부를 반환 true 합니다.

    ref struct R {
    R(R% r) {}
    };
    
    int main() {
    System::Console::WriteLine(__has_copy(R));
    }
    
  • __has_finalizer(type)

    (C++/CX에서는 지원되지 않습니다.) CLR 형식에 종료자가 있는지 여부를 반환 true 합니다. 자세한 내용은 방법: 클래스 및 구조체 정의 및 사용(C++/CLI)의 소멸자 및 종료자를 참조하세요.

    using namespace System;
    ref struct R {
    ~R() {}
    protected:
    !R() {}
    };
    
    int main() {
    Console::WriteLine(__has_finalizer(R));
    }
    
  • __has_nothrow_assign(type)

    복사 할당 연산자에 빈 예외 사양이 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {
    void operator=(S& r) throw() {}
    };
    
    int main() {
    __has_nothrow_assign(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_nothrow_constructor(type)

    기본 생성자에 빈 예외 사양이 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {
    S() throw() {}
    };
    
    int main() {
    __has_nothrow_constructor(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_nothrow_copy(type)

    복사 생성자에 빈 예외 사양이 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {
    S(S& r) throw() {}
    };
    
    int main() {
    __has_nothrow_copy(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_trivial_assign(type)

    형식에 컴파일러에서 생성된 간단한 할당 연산자가 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {};
    
    int main() {
    __has_trivial_assign(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_trivial_constructor(type)

    형식에 간단한 컴파일러 생성자가 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {};
    
    int main() {
    __has_trivial_constructor(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_trivial_copy(type)

    형식에 컴파일러에서 생성된 간단한 복사 생성자가 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {};
    
    int main() {
    __has_trivial_copy(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_trivial_destructor(type)

    형식에 간단한 컴파일러 생성 소멸자가 있는지를 반환 true 합니다.

    // has_trivial_destructor.cpp
    #include <stdio.h>
    struct S {};
    
    int main() {
    __has_trivial_destructor(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __has_user_destructor(type)

    플랫폼 또는 네이티브 형식에 사용자 선언 소멸자가 있는지 여부를 반환 true 합니다.

    // has_user_destructor.cpp
    
    using namespace System;
    ref class R {
    ~R() {}
    };
    
    int main() {
    Console::WriteLine(__has_user_destructor(R));
    }
    
  • __has_virtual_destructor(type)

    형식에 가상 소멸자가 있는지를 반환 true 합니다.

    __has_virtual_destructor는 플랫폼 형식에 대해서도 작동하며, 플랫폼 형식의 모든 사용자 정의 소멸자는 가상 소멸자입니다.

    // has_virtual_destructor.cpp
    #include <stdio.h>
    struct S {
    virtual ~S() {}
    };
    
    int main() {
    __has_virtual_destructor(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_abstract(type)

    형식이 추상 형식인지를 반환 true 합니다. 네이티브 추상 형식에 대한 자세한 내용은 추상 클래스를 참조하세요.

    __is_abstract는 플랫폼 형식에 대해서도 작동합니다. 하나 이상의 멤버가 있는 인터페이스는 하나 이상의 추상 멤버가 있는 참조 형식과 마찬가지로 추상 형식입니다. 추상 플랫폼 형식에 대한 자세한 내용은 abstract를 참조하세요.

    // is_abstract.cpp
    #include <stdio.h>
    struct S {
    virtual void Test() = 0;
    };
    
    int main() {
    __is_abstract(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_base_of( base , derived )

    첫 번째 형식이 두 번째 형식의 기본 클래스이거나 두 형식이 같은지 여부를 반환 true 합니다.

    __is_base_of는 플랫폼 형식에 대해서도 작동합니다. 예를 들어 첫 번째 형식이 인터페이스 클래스이고 두 번째 형식이 인터페이스를 구현하는 경우 반환 true 됩니다.

    // is_base_of.cpp
    #include <stdio.h>
    struct S {};
    struct T : public S {};
    
    int main() {
    __is_base_of(S, T) == true ?
    printf("true\n") : printf("false\n");
    
    __is_base_of(S, S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_class(type)

    형식이 네이티브 클래스 또는 구조체인지 여부를 반환 true 합니다.

    #include <stdio.h>
    struct S {};
    
    int main() {
    __is_class(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_convertible_to( from , to )

    첫 번째 형식을 두 번째 형식으로 변환할 수 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {};
    struct T : public S {};
    
    int main() {
    S * s = new S;
    T * t = new T;
    s = t;
    __is_convertible_to(T, S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_delegate(type)

    대리자인지를 type 반환 true 합니다. 자세한 내용은 delegate(C++/CLI 및 C++/CX)를 참조하세요.

    delegate void MyDel();
    int main() {
    System::Console::WriteLine(__is_delegate(MyDel));
    }
    
  • __is_empty(type)

    true 형식에 인스턴스 데이터 멤버가 없는 경우 반환합니다.

    #include <stdio.h>
    struct S {
    int Test() {}
    static int i;
    };
    int main() {
    __is_empty(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_enum(type)

    형식이 네이티브 열거형인지를 반환 true 합니다.

    // is_enum.cpp
    #include <stdio.h>
    enum E { a, b };
    
    struct S {
    enum E2 { c, d };
    };
    
    int main() {
    __is_enum(E) == true ?
    printf("true\n") : printf("false\n");
    
    __is_enum(S::E2) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_interface_class(type)

    플랫폼 인터페이스를 전달한 경우 반환 true 합니다. 자세한 내용은 인터페이스 클래스를 참조하세요.

    // is_interface_class.cpp
    
    using namespace System;
    interface class I {};
    int main() {
    Console::WriteLine(__is_interface_class(I));
    }
    
  • __is_pod(type)

    형식이 생성자나 프라이빗 또는 보호된 비정적 멤버, 기본 클래스 및 가상 함수가 없는 클래스 또는 공용 구조체인지를 반환 true 합니다. POD에 대한 자세한 내용은 C++ 표준, 섹션 8.5.1/1, 9/4 및 3.9/10을 참조하세요.

    __is_pod는 기본 형식에 대해 false를 반환합니다.

    #include <stdio.h>
    struct S {};
    
    int main() {
    __is_pod(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_polymorphic(type)

    네이티브 형식에 가상 함수가 있는지를 반환 true 합니다.

    #include <stdio.h>
    struct S {
    virtual void Test(){}
    };
    
    int main() {
    __is_polymorphic(S) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_ref_array(type)

    플랫폼 배열을 전달한 경우 반환 true 합니다. 자세한 내용은 배열을 참조하세요.

    using namespace System;
    int main() {
    array<int>^ x = gcnew array<int>(10);
    Console::WriteLine(__is_ref_array(array<int>));
    }
    
  • __is_ref_class(type)

    true 참조 클래스를 전달한 경우 반환합니다. 사용자 정의 참조 형식에 대한 자세한 내용은 클래스 및 구조체를 참조하세요.

    using namespace System;
    ref class R {};
    int main() {
    Console::WriteLine(__is_ref_class(Buffer));
    Console::WriteLine(__is_ref_class(R));
    }
    
  • __is_sealed(type)

    봉인된 것으로 표시된 플랫폼 또는 네이티브 형식을 전달한 경우 반환 true 합니다. 자세한 내용은 sealed를 참조하세요.

    ref class R sealed{};
    int main() {
    System::Console::WriteLine(__is_sealed(R));
    }
    
  • __is_simple_value_class(type)

    true 가비지 수집 힙에 대한 참조가 없는 값 형식을 전달한 경우 반환합니다. 사용자 정의 값 형식에 대한 자세한 내용은 클래스 및 구조체를 참조하세요.

    using namespace System;
    ref class R {};
    value struct V {};
    value struct V2 {
    R ^ r;   // not a simple value type
    };
    
    int main() {
    Console::WriteLine(__is_simple_value_class(V));
    Console::WriteLine(__is_simple_value_class(V2));
    }
    
  • __is_union(type)

    형식이 공용 구조체이면 반환 true 합니다.

    #include <stdio.h>
    union A {
    int i;
    float f;
    };
    
    int main() {
    __is_union(A) == true ?
    printf("true\n") : printf("false\n");
    }
    
  • __is_value_class(type)

    true 값 형식을 전달한 경우 반환합니다. 사용자 정의 값 형식에 대한 자세한 내용은 클래스 및 구조체를 참조하세요.

    value struct V {};
    
    int main() {
    System::Console::WriteLine(__is_value_class(V));
    }
    

Windows 런타임

설명

이 플랫폼에서 종료자를 지원하지 않으므로 __has_finalizer(type) 형식 특성은 지원되지 않습니다.

요구 사항

컴파일러 옵션: /ZW

공용 언어 런타임

설명

(이 기능에는 플랫폼별 설명이 없습니다.)

요구 사항

컴파일러 옵션: /clr

예제

예제

다음 코드 예제에서는 클래스 템플릿을 사용하여 /clr 컴파일에 대한 컴파일러 형식 특성을 노출하는 방법을 보여 줍니다. 자세한 내용은 Windows 런타임 및 관리형 템플릿을 참조하세요.

// compiler_type_traits.cpp
// compile with: /clr
using namespace System;

template <class T>
ref struct is_class {
   literal bool value = __is_ref_class(T);
};

ref class R {};

int main () {
   if (is_class<R>::value)
      Console::WriteLine("R is a ref class");
   else
      Console::WriteLine("R is not a ref class");
}
R is a ref class

참고 항목

.NET 및 UWP용 구성 요소 확장