C++ 11標準指定了一個類型特徵std::alignment_of<T>
,它簡單地返回值alignof(T)
。sizeof(T)值的標準類型特徵
對於sizeof
運算符有沒有類似的特徵?我是否錯過了它,還是僅僅是錯過了標準,還是有一些模糊的技術原因,爲什麼它沒有被指定?
顯然創造這樣的特質是微不足道的,但我無法想象在介紹std::alignment_of
時不會考慮它。
對於上下文,我有一個自定義類型特徵,用於在應用於類型列表時獲取單個特徵的最大值。
template <template<class> class Trait, typename F, typename... T>
struct trait_max
: std::integral_constant<decltype(Trait<F>::value),
(Trait<F>::value > trait_max<Trait, T...>::value) ? Trait<F>::value : trait_max<Trait, T...>::value>
{ };
template <template<class> class Trait, typename F>
struct trait_max<Trait, F>
: std::integral_constant<decltype(Trait<F>::value), Trait<F>::value>
{ };
這個特點是當你需要知道的最大的一組類型的,像這樣非常方便:
auto max_align = traits_max<std::alignment_of, int, float, std::string>::value;
auto max_size = traits_max<std::size_of, int, float, std::string>::value; // doesn't exist
錯字在你的代碼,'特質'應該是'?'操作之後'特質'。我只注意到,因爲這太棒了,我非常喜歡它。 –
std :: alignment_of'在標準中的唯一原因是因爲它是TR1的一部分。 –
Simple
幸運的是,'template struct size_of:std :: integral_constant {};'也不難寫。 –
Yakk