std::alignment_of
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct alignment_of; |
(C++11 起) | |
提供成員常量 value,其值等於型別 T
的對齊要求,如同透過 alignof 表示式獲得。如果 T
是陣列型別,則返回元素型別的對齊要求。如果 T
是引用型別,則返回所引用型別的對齊要求。
如果 alignof(T) 不是有效表示式,則行為未定義。
如果程式為 std::alignment_of
或 std::alignment_of_v
(C++17 起) 新增特化,則行為未定義。
目錄 |
[編輯] 輔助變數模板
template< class T > constexpr std::size_t alignment_of_v = alignment_of<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
alignof(T) (public static 成員常量) |
成員函式
operator std::size_t |
將物件轉換為 std::size_t,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
std::size_t |
型別
|
std::integral_constant<std::size_t, value> |
[編輯] 可能實現
template<class T> struct alignment_of : std::integral_constant<std::size_t, alignof(T)> {}; |
[編輯] 注意
此型別特性早於 alignof 關鍵字,後者可用於以更簡潔的方式獲取相同的值。
[編輯] 示例
執行此程式碼
#include <cstdint> #include <iostream> #include <type_traits> struct A {}; struct B { std::int8_t p; std::int16_t q; }; int main() { std::cout << std::alignment_of<A>::value << ' '; std::cout << std::alignment_of<B>::value << ' '; std::cout << std::alignment_of<int>() << ' '; // alt syntax std::cout << std::alignment_of_v<double> << '\n'; // c++17 alt syntax }
可能的輸出
1 2 4 8
[編輯] 參閱
alignof (C++11 起) |
查詢型別的對齊要求 (運算子) |
alignas (C++11 起) |
指定變數的儲存應按特定量對齊 (說明符) |
(自 C++11)(C++23 中已棄用) |
定義適合用作給定大小型別的未初始化儲存的型別 (類模板) |
(自 C++11)(C++23 中已棄用) |
定義適合用作所有給定型別的未初始化儲存的型別 (類模板) |
(C++11) |
微不足道的型別,其對齊要求與其他任何標量型別一樣大 (typedef) |