名稱空間
變體
操作

std::is_pointer_interconvertible_with_class

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <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() { }

[編輯] 參閱

檢查型別是否為標準佈局型別
(類模板) [編輯]
檢查型別是否為非靜態成員物件指標
(類模板) [編輯]