types_is_corresponding_member
types_is_corresponding_member (서로 대응하는 멤버인지 판단)
이 페이지는 C++20에서 도입된 std::is_corresponding_member 함수 템플릿에 대해 설명해요. 이 함수는 두 표준 레이아웃 타입의 공통 초기 시퀀스에서 두 멤버 포인터가 서로 대응하는 멤버를 가리키는지 판단해요. 멤버 포인터의 타입이 명시적으로 주어지지 않으면 상속 관계로 인해 예상치 못한 결과가 나올 수 있으므로 주의해야 해요.
출처: cppreference
본문
정의
<type_traits> 헤더에 정의되어 있어요.
<type_traits> 헤더에 정의됨 |
||
|---|---|---|
template < class S1 , class S2 , class M1 , class M2 > constexpr bool is_corresponding_member ( M1 S1 ::* mp , M2 S2 ::* mq ) noexcept ; |
(C++20부터) |
mp와 mq가 S1과 S2의 공통 초기 시퀀스(common initial sequence)에서 서로 대응하는 멤버를 가리키는지 판단해요. S1이나 S2 중 하나라도 불완전한 타입이면 프로그램은 ill-formed가 돼요.
S1이나 S2가 StandardLayoutType이 아니거나, M1이나 M2가 객체 타입이 아니거나, mp나 mq가 nullptr이면 결과는 항상 false예요.
Parameters
| mp, mq | - | 검출할 멤버 포인터 |
|---|
Return value
S1과 S2의 공통 초기 시퀀스에서 mp와 mq가 서로 대응하는 멤버를 가리키면 true, 그렇지 않으면 false를 반환해요.
Notes
멤버 포인터 표현식 &S::m의 타입이 항상 M S::*인 것은 아니에요. m이 S의 기반 클래스에서 상속된 멤버일 수 있기 때문이죠. 템플릿 인자를 명시하면 예상치 못한 결과를 피할 수 있어요.
Example
#include <type_traits>
struct Foo
{
int x;
double d;
};
struct Bar
{
int y;
double z;
};
struct Baz : Foo, Bar {}; // not standard-layout
static_assert(
std::is_same_v<decltype(&Baz::x), int Foo::*> == true &&
std::is_same_v<decltype(&Baz::y), int Bar::*> == true &&
std::is_corresponding_member(&Foo::x, &Bar::y) == true &&
std::is_corresponding_member(&Foo::d, &Bar::z) == true &&
std::is_corresponding_member(&Baz::x, &Baz::y) == true &&
std::is_corresponding_member<Baz, Baz, int, int>(&Baz::x, &Baz::y) == false
);
int main() {}
See also
| is_standard_layout (C++11) | 표준 레이아웃 타입인지 확인해요 (클래스 템플릿) [edit] |
|---|---|
| is_layout_compatible (C++20) | 두 타입이 레이아웃 호환인지 확인해요 (클래스 템플릿) [edit] |
| is_member_object_pointer (C++11) | 비정적 멤버 객체 포인터 타입인지 확인해요 (클래스 템플릿) [edit] |