다음을 통해 공유


형식 특성에 대한 컴파일러 지원(C++ 구성 요소 확장)

컴파일러는 형식 특성을 지원하여, 컴파일 타임에 형식의 다양한 특성을 나타냅니다.

모든 런타임

설명

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

다음 표는 컴파일러에서 지원하는 형식의 특성을 나열합니다. 형식 특성의 이름으로 지정 된 조건에 맞지 않을 경우 특성 형식이 모두 false 로 반환됩니다.

(표의 설명 열에서 코드 예제는 에만 기록됩니다 . 달리 명시되지 않는 한 해당 유형의 특성은 Visual C++ 구성 요소 확장에서 지원됩니다. 용어, "플랫폼 유형"은 Windows 런타임 형식 또는 공용 언어 런타임 형식 중 하나를 의미합니다.)

Type Trait

설명

__has_assign(type)

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

// has_assign.cpp
// compile with: /clr
ref struct R {
   void operator=(R% r) {}
};
int main() {
   System::Console::WriteLine(__has_assign(R));
}

__has_copy(type)

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

// has_copy.cpp
// compile with: /clr
ref struct R {
   R(R% r) {}
};
int main() {
   System::Console::WriteLine(__has_copy(R));
}

__has_finalizer(type)

(Visual C++ 구성 요소 확장에서는 지원되지 않습니다.) CLR 형식에 종료자가 있으면 true를 반환합니다. 자세한 내용은 소멸자 및 종료자에서를 참조하십시오.

// has_finalizer.cpp
// compile with: /clr
using namespace System;
ref struct R {
   ~R() {}
protected:
   !R() {}
};
int main() {
   Console::WriteLine(__has_finalizer(R));
}

__has_nothrow_assign(type)

복사 할당 연산자는 빈 예외 규정을 가지면 true를 반환합니다.

// has_nothrow_assign.cpp
#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를 반환합니다.

// has_nothrow_constructor.cpp
#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를 반환합니다.

// has_nothrow_copy.cpp
#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를 반환합니다.

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

__has_trivial_constructor(type)

형식이 특수, 컴파일러 생성 생성자를 가질 경우 true를 반환합니다.

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

__has_trivial_copy(type)

형식이 특수, 컴파일러 복사 생성자를 가질 경우 true를 반환합니다.

// has_trivial_copy.cpp
#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
// compile with: /clr
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를 반환합니다. 추상 기본 형식에 대한 자세한 내용은 abstract(C++ 구성 요소 확장)을 참조하십시오.

__is_abstract또한 플랫폼 유형에 대해 작동합니다. 적어도 하나의 추상 부재와 참조 형식으로 적어도 하나의 멤버를 가진 인터페이스는 추상 타입입니다. 추상 플랫폼 형식에 대한 자세한 내용은 추상 클래스 (C++)을 참조하십시오.

// 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은 또한 플랫폼 형식에 대해 작동합니다. 예를 들어, 첫 번째 형식이 interface 클래스(C++ 구성 요소 확장)이면 반환됩니다. 그리고 두 번째 형식은 인터페이스를 구현 합니다.

// 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를 반환합니다.

// is_class.cpp
#include <stdio.h>
struct S {};
int main() {
   __is_class(S) == true ? 
      printf("true\n") : printf("false\n");
}

__is_convertible_to(from, to)

첫 번째 유형은 두 번째 유형으로 변환할 수 있으면 true를 반환합니다.

// is_convertible_to.cpp
#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++ 구성 요소 확장)을 참조하십시오.

// is_delegate.cpp
// compile with: /clr
delegate void MyDel();
int main() {
   System::Console::WriteLine(__is_delegate(MyDel));
}

__is_empty(type)

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

// is_empty.cpp
#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를 반환합니다. 자세한 내용은 interface 클래스(C++ 구성 요소 확장)을 참조하십시오.

// is_interface_class.cpp
// compile with: /clr
using namespace System;
interface class I {};
int main() {
   Console::WriteLine(__is_interface_class(I));
}

__is_pod(type)

형식이 기본 클래스 및 가상 함수가 없는 클래스 또는 공용 생성자 또는 전용 또는 보호된 비정적 멤버일 경우 true를 반환합니다. PODs의 자세한 내용은 8.5.1/1, 9/4, and 3.9/10 섹션을 참조하십시오.

__is_pod는 기본 형식으로 false를 반환합니다.

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

__is_polymorphic(type)

네이티브 형식가 가상 함수일 경우 true를 반환합니다.

// is_polymorphic.cpp
#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를 반환합니다. 자세한 내용은 배열(C++ 구성 요소 확장)을 참조하십시오.

// is_ref_array.cpp
// compile with: /clr
using namespace System;
int main() {
   array<int>^ x = gcnew array<int>(10);
   Console::WriteLine(__is_ref_array(array<int>));
}

__is_ref_class(type)

참조 클래스에 전달된 경우 true를 반환합니다. 사용자 정의 참조 형식에 대한 자세한 내용은 클래스 및 구조체(C++ 구성 요소 확장)을 참조하십시오.

// is_ref_class.cpp
// compile with: /clr
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(C++ 구성 요소 확장)을 참조하십시오.

// is_sealed.cpp
// compile with: /clr
ref class R sealed{};
int main() {
   System::Console::WriteLine(__is_sealed(R));
}

__is_simple_value_class(type)

가비지 수집 된 힙에 대한 참조가 포함된 값 형식을 전달하는 경우 true를 반환합니다. 사용자 정의 값 형식에 대한 자세한 내용은 클래스 및 구조체(C++ 구성 요소 확장)을 참조하십시오.

// is_simple_value_class.cpp
// compile with: /clr
using namespace System;
ref class R {};
value struct V {};
value struct V2 {
   R ^ r;   // not a simnple value type
};
int main() {
   Console::WriteLine(__is_simple_value_class(V));
   Console::WriteLine(__is_simple_value_class(V2));
}

__is_union(type)

형식이 union 경우 true를 반환합니다.

// is_union.cpp
#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를 반환합니다. 사용자 정의 값 형식에 대한 자세한 내용은 클래스 및 구조체(C++ 구성 요소 확장)을 참조하십시오.

// is_value_class.cpp
// compile with: /clr
value struct V {};
int main() {
   System::Console::WriteLine(__is_value_class(V));
}

Windows 런타임

설명

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

요구 사항

컴파일러 옵션: /ZW

공용 언어 런타임

설명

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

요구 사항

컴파일러 옵션: /clr

예제

예제

다음 코드 예제에서는 /clr 컴파일을 위한 컴파일러 형식의 특성을 노출하는 클래스 템플릿을 사용하는 방법을 보여줍니다. 자세한 내용은 Windows 런타임 및 관리되는 템플릿(C++ 구성 요소 확장)을 참조하십시오.

// 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");
}

Output

  

참고 항목

개념

런타임 플랫폼의 구성 요소 확장