std::alignment_of
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct alignment_of; |
(C++11以降) | |
型Tのアラインメント要求に等しいメンバ定数valueを提供します。これは、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 [static] |
alignof(T) (公開静的メンバ定数) |
メンバ関数
| operator std::size_t |
オブジェクトを std::size_t に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
std::size_t |
type
|
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) |