Namespaces
Variants

반영 연산자 (C++26부터)

cppreference.net에서
 
 
C++ 언어
일반 주제
흐름 제어
조건부 실행문
if
반복문
점프문
함수
함수 선언
람다 함수 표현식
inline 지정자
동적 예외 사양 (C++17까지*)
noexcept 지정자 (C++11)
예외
네임스페이스
타입
지정자
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
저장 기간 지정자
초기화
표현식
대체 표현
리터럴
부울 - 정수 - 부동소수점
문자 - 문자열 - nullptr (C++11)
사용자 정의 (C++11)
유틸리티
어트리뷰트 (C++11)
타입
typedef 선언
타입 별칭 선언 (C++11)
캐스트
메모리 할당
클래스
클래스별 함수 속성
explicit (C++11)
static

특별 멤버 함수
템플릿
기타
 
 

단항 ^^ 연산자, 반영 연산자라고 불리며, 주어진 언어 구문의 반영을 std::meta::info 타입으로 생성합니다.

구문

^^ :: (1)
^^ 반영 이름 (2)
^^ 타입 식별자 (3)
^^ 식별 표현식 (4)
반영 이름 - 식별자, 선택적으로 한정됨, 선택적으로 템플릿 명확화 사용. 이 형식은 타입 식별자 및 식별 표현식
보다 우선합니다.타입 식별자 - a 타입 식별자
식별 표현식 - a 식별 표현식

구문적으로 ^^의 피연산자가 될 수 있는 가장 긴 토큰 시퀀스가 파싱됩니다. 결과가 템플릿을 나타내는 경우 표현식 바로 뒤에 <가 오면 안 됩니다.

static_assert(std::meta::is_type(^^int())); // ^^int() represents the type int()
                                            // not a function call to ^^int

template<bool> struct X {};
consteval bool operator<(std::meta::info, X<false>) { return false; }
consteval void g(std::meta::info r, X<false> xv) {
    r == ^^int && true; // error: ^^ applies to the type-id int&&
    r == ^^int & true; // error: ^^ applies to the type-id int&
    r == (^^int) && true; // OK
    r == ^^int &&&& true; // error: int &&&& is not a valid type-id
    ^^X < xv; // error: reflect-expression that represents a template is followed by <
    (^^X) < xv; // OK
    ^^X<true> < xv; // OK
}

설명

1) 표현식 ^^::은 전역 네임스페이스를 나타냅니다.
2) ^^reflection-name에서 반영 이름이 조회되며, 표현은 다음과 같이 결정됩니다:
  • 템플릿 명확화가 있으면, 결과는 주입 클래스 이름이 명명한 클래스 템플릿을 나타냅니다.
  • 그렇지 않으면, 주입 클래스 이름은 타입 이름으로 간주할 때 모호하지 않아야 합니다(그렇지 않으면 표현식은 유효하지 않습니다). 결과는 그렇게 명명된 타입을 나타냅니다.
  • 그렇지 않고, 조회가 오버로드 집합을 찾으면, 해당 오버로드 집합은 고유한 함수 템플릿의 선언만 포함해야 합니다. 결과는 해당 함수 템플릿을 나타냅니다.
  • 그렇지 않고, 조회가 클래스 템플릿, 변수 템플릿, 또는 별칭 템플릿을 찾으면, 결과는 해당 템플릿을 나타냅니다.
  • 그렇지 않고, 조회가 타입 템플릿 매개변수를 찾으면, 결과는 해당 인자의 타입을 나타냅니다.
  • 그렇지 않고, 조회가 typedef 이름 또는 타입 별칭을 찾으면, 결과는 해당 타입 별칭을 나타냅니다.
template<typename T>
struct S {
    static constexpr std::meta::info r = ^^T;
    using type = T;
};
static_assert(S<int>::r == ^^int); // OK
static_assert(^^S<int>::type != ^^int); // OK

typedef struct X {} Y;
typedef struct Z {} Z;
constexpr std::meta::info e = ^^Y; // OK, represents the type alias Y
constexpr std::meta::info f = ^^Z; // OK, represents the type alias Z, not the type
  • 그렇지 않고, 조회가 클래스 타입 또는 열거형 타입을 찾으면, 결과는 해당 타입을 나타냅니다.
  • 그렇지 않고, 조회가 익명 공용체의 클래스 멤버를 찾으면, 결과는 해당 멤버를 나타냅니다.
  • 그렇지 않으면, 반영 이름은 식별 표현식으로 간주됩니다(아래 형식 (4) 참조).
3) 표현식 ^^type-id은 다음과 같이 결정된 엔티티를 나타냅니다:
  • 타입 식별자가 플레이스홀더 타입(예: auto)을 지정하면, 표현식은 유효하지 않습니다.
  • 그렇지 않고, 타입 식별자가 템플릿 인자 목록이 있는 별칭 템플릿을 명명하면, 결과는 그렇게 명명된 타입 별칭을 나타냅니다. (다른 형태의 타입 별칭 이름은 반영 이름으로 처리되며, 결과도 타입 별칭을 나타냅니다.)
  • 그렇지 않으면, 결과는 타입 식별자가 나타내는 타입을 나타냅니다.
4) 표현식 ^^id-expression은 다음과 같이 결정된 엔티티를 나타냅니다:
  • 식별 표현식이 다음을 나타내면:
표현식은 유효하지 않습니다.
  • 그렇지 않고, 식별 표현식이 오버로드 집합을 나타내면, 오버로드 해결이 고유한 함수를 선택해야 합니다. 결과는 해당 함수를 나타냅니다.
template<typename T> void fn() requires (^^T != ^^int);
template<typename T> void fn() requires (^^T == ^^int);
template<typename T> void fn() requires (sizeof(T) == sizeof(int));
constexpr std::meta::info a = ^^fn<char>; // OK
constexpr std::meta::info b = ^^fn<int>;  // error: ambiguous

피연산자는 평가되지 않은 피연산자입니다.

참고

반영 값은 std::meta::reflect_constant, std::meta::reflect_object, std::meta::reflect_function, 또는 기본 초기화 std::meta::info로도 생성할 수 있습니다. 또한, 기존 <std::meta::info> 객체로 메타 함수를 호출하여 반영 값을 얻을 수 있습니다.

타입 별칭이 타입 식별자의 일부로 나타나는 경우, 반영 연산자는 타입 별칭을 보존하지 않습니다.

using T = int;
static_assert(^^T != ^^int);
static_assert(^^T& == ^^int&);
static_assert(^^T const == ^^const int);

예제

int arr[] = {1, 2, 3};
auto [a1, a2, a3] = arr;
[[=1]] void fn(int n);
enum Enum { A };
using Alias = int;
struct S { int mem; };
template<auto> struct TCls {};
template<auto> void TFn();
template<auto> int TVar;
template<auto N> using TAlias = TCls<N>;
template<auto> concept Concept = true;
namespace NS {}
namespace NSAlias = NS;

constexpr auto r_arr   = ^^arr; // represents a variable
constexpr auto r_sb    = ^^a3; // represents a structured binding
constexpr auto r_fn    = ^^fn; // represents a function
constexpr auto r_enum  = ^^Enum::A; // represents an enumerator
constexpr auto r_alias = ^^Alias; // represents a type alias
constexpr auto r_type  = ^^S; // represents a type
constexpr auto r_mem   = ^^S::mem; // represents a class member
constexpr auto r_tcls  = ^^TCls; // represents a class template
constexpr auto r_tfn   = ^^TFn; // represents a function template
constexpr auto r_tvar  = ^^TVar; // represents a variable template
constexpr auto r_ttype = ^^TAlias; // represents an alias template
constexpr auto r_cncpt = ^^Concept; // represents a concept
constexpr auto r_ns    = ^^NS; // represents a namespace
constexpr auto r_ns2   = ^^NSAlias; // represents a namespace alias

int main() {}

같이 보기

값 또는 템플릿 매개변수 객체를 나타내는 반영을 반환하며, 상수 템플릿 인자로 사용하기 적합함
(함수 템플릿)
객체를 나타내는 반영을 반환하며, 상수 템플릿 인자로 사용하기 적합함
(함수 템플릿)
함수를 나타내는 반영을 반환하며, 상수 템플릿 인자로 사용하기 적합함
(함수 템플릿)