std::is_pointer_interconvertible_with_class
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class S, class M > constexpr bool is_pointer_interconvertible_with_class( M S::* mp ) noexcept; |
(C++20 起) | |
給定型別為 S
的物件 s
,確定 s.*mp 是否引用 s
的子物件,以及 s
是否能與其子物件 s.*mp 指標互轉。如果 S
不是一個完整型別,程式格式錯誤。
如果 S
不是 標準佈局型別 (StandardLayoutType),或 M
不是物件型別,或 mp
等於 nullptr,則結果始終為 false。
目錄 |
[編輯] 引數
mp | - | 要檢測的成員指標 |
[編輯] 返回值
如果 s.*mp 引用 s
的子物件,並且 s
能與其子物件 s.*mp 指標互轉,則為 true,否則為 false,其中 s
是型別為 S
的有效左值。
[編輯] 注意
成員指標表示式 &S::m 的型別並不總是 M S::*,其中 m
的型別是 M
,因為 m
可能是從 S
的基類繼承的成員。可以指定模板引數以避免潛在的意外結果。
如果存在型別為 M S::* 的值 mp
,使得 std::is_pointer_interconvertible_with_class(mp) == true,則 reinterpret_cast<M&>(s) 具有良好定義的,並且它引用與 s.*mp 相同的子物件,其中 s
是型別為 S
的有效左值。
在常見的平臺上,如果 std::is_pointer_interconvertible_with_class(mp) == true,則 mp
的位模式全部為零。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_is_pointer_interconvertible |
201907L |
(C++20) | 指標互轉特性
|
[編輯] 示例
執行此程式碼
#include <type_traits> struct Foo { int x; }; struct Bar { int y; }; struct Baz : Foo, Bar {}; // not standard-layout static_assert( not std::is_same_v<decltype(&Baz::x), int Baz::*> ); static_assert( std::is_pointer_interconvertible_with_class(&Baz::x) ); static_assert( not std::is_pointer_interconvertible_with_class<Baz, int>(&Baz::x) ); int main() { }
[編輯] 參閱
(C++11) |
檢查型別是否為標準佈局型別 (類模板) |
(C++11) |
檢查型別是否為非靜態成員物件指標 (類模板) |