看看这个模板。
template < typename T1, typename T2, typename T3 >
struct Alignement {
T1 first;
T2 second;
T3 third;
};
int main() {
Alignement<char, int, double> a1;
Alignement<char, double, int> a2;
assert( sizeof(a1) < sizeof(a2) );
return 0;
}
显然断言成立。在这种情况下,次优排序会导致内存使用量增加 50%。
我的问题是,除了请用户自己处理它之外,还有什么方法可以解决它并在模板结构中正确排序类型(如果他不知道大小,这会让他遇到同样的问题)事先他的类型)?
我的想法是在编译时使用宏或 TMP 动态生成最佳排序,但我对这些技术没有适当的了解。或者也许一群部分特化的模板可以完成工作?
关键方面是为客户端保留AlignedObject.first
语法。
对于我的具体情况,我正在寻找正好 3 个参数(3!可能的排序)的解决方案,但通用解决方案(包括可变长度模板)将会很有趣。
最佳答案
For my specific case, I'm looking for solution for exactly 3 parameters (3! possible orderings) but general solution (including variadic-length-templates) would be interesting to see.
我提出了一个通用的解决方案:一个可变参数类型排序器和一个可变参数 Alignement
使用它。
按照 Peter 的建议,我们的想法是对类型进行排序,将较大的类型放在前面。
我使用 C++17 因为新的模板折叠允许我使用 C++11 因为 OP 必须使用仅兼容 C++11 的编译器才能使极其简单 一个类型特征,表明列表的第一种类型是否是较大的类型(根据 sizeof()
)。我维护、评论了原始的 C++17 版本
// iftb = is first type bigger ?
// original C++17 version
//
// template <typename T0, typename ... Ts>
// struct iftb
// : public std::integral_constant<bool,((sizeof(Ts) <= sizeof(T0)) && ...)>
// { };
template <typename ...>
struct iftb;
template <typename T0>
struct iftb<T0> : public std::true_type
{ };
template <typename T0, typename T1, typename ... Ts>
struct iftb<T0, T1, Ts...>
: public std::integral_constant<bool,
(sizeof(T1) <= sizeof(T0)) && iftb<T0, Ts...>::value>
{ };
现在有一个类型特征来了解类型容器是否包含有序类型列表
// ifctb = is first contained type bigger ?
template <typename>
struct ifctb;
template <template <typename ...> class C, typename ... Tc>
struct ifctb<C<Tc...>> : public iftb<Tc...>
{ };
现在类型排序器很容易编写(但不是特别有效;抱歉)
// to = type orderer
template <typename, typename Cd, bool = ifctb<Cd>::value>
struct to;
template <template <typename...> class C, typename ... To,
typename T0, typename ... Tu>
struct to<C<To...>, C<T0, Tu...>, true> : public to<C<To..., T0>, C<Tu...>>
{ };
template <template <typename...> class C, typename ... To,
typename T0, typename ... Tu>
struct to<C<To...>, C<T0, Tu...>, false> : public to<C<To...>, C<Tu..., T0>>
{ };
template <template <typename...> class C, typename ... To, typename T>
struct to<C<To...>, C<T>, true>
{ using type = C<To..., T>; };
现在我提出一个索引包装器,必须通过部分特化来定义 first
, second
和third
(等等,如果你想扩展解决方案)
template <std::size_t, typename>
struct wrapper;
template <typename T>
struct wrapper<0U, T>
{ T first; };
template <typename T>
struct wrapper<1U, T>
{ T second; };
template <typename T>
struct wrapper<2U, T>
{ T third; };
我们需要std::index_sequence
和std::make_index_sequence
仅从 C++14 开始可用;但 OP 必须在仅兼容 C++11 的编译器中编译此代码,因此我建议使用一个兼容 C++11 的简单模拟
// std::index_sequence and std::make_index_sequence simplified emulators
template <std::size_t...>
struct indexSequence
{ using type = indexSequence; };
template <typename, typename>
struct concatSequences;
template <std::size_t... S1, std::size_t... S2>
struct concatSequences<indexSequence<S1...>, indexSequence<S2...>>
: public indexSequence<S1..., ( sizeof...(S1) + S2 )...>
{ };
template <std::size_t N>
struct makeIndexSequenceH
: public concatSequences<
typename makeIndexSequenceH<(N>>1)>::type,
typename makeIndexSequenceH<N-(N>>1)>::type>::type
{ };
template<>
struct makeIndexSequenceH<0> : public indexSequence<>
{ };
template<>
struct makeIndexSequenceH<1> : public indexSequence<0>
{ };
template <std::size_t N>
using makeIndexSequence = typename makeIndexSequenceH<N>::type;
在std::tuple
的帮助下,std::index_sequence
和std::make_index_sequence
indexSequence
和makeIndexSequence
(std::index_sequence
和 std::make_index_sequence
的 C++11 兼容简化模拟),我添加了几个帮助器 struct
为Alignement
template <typename>
struct AlH2;
template <typename ... Ts>
struct AlH2<std::tuple<Ts...>> : public Ts...
{ };
template <typename...>
struct AlH1;
template <std::size_t ... Is, typename ... Ts>
struct AlH1<indexSequence<Is...>, Ts...>
: public AlH2<typename to<std::tuple<>,
std::tuple<wrapper<Is, Ts>...>>::type>
{ };
现在Alignement
可以写成
template <typename ... Ts>
struct Alignement
: public AlH1<makeIndexSequence<sizeof...(Ts)>, Ts...>
{ };
以下是完整的(我记得:C++17) C++11 编译示例,其中包含一些 assert()
来验证顺序是否正确。
#include <tuple>
#include <cassert>
#include <iostream>
#include <type_traits>
// std::index_sequence and std::make_index_sequence simplified emulators
template <std::size_t...>
struct indexSequence
{ using type = indexSequence; };
template <typename, typename>
struct concatSequences;
template <std::size_t... S1, std::size_t... S2>
struct concatSequences<indexSequence<S1...>, indexSequence<S2...>>
: public indexSequence<S1..., ( sizeof...(S1) + S2 )...>
{ };
template <std::size_t N>
struct makeIndexSequenceH
: public concatSequences<
typename makeIndexSequenceH<(N>>1)>::type,
typename makeIndexSequenceH<N-(N>>1)>::type>::type
{ };
template<>
struct makeIndexSequenceH<0> : public indexSequence<>
{ };
template<>
struct makeIndexSequenceH<1> : public indexSequence<0>
{ };
template <std::size_t N>
using makeIndexSequence = typename makeIndexSequenceH<N>::type;
// iftb = is first type bigger ?
// original C++17 version
//
// template <typename T0, typename ... Ts>
// struct iftb
// : public std::integral_constant<bool,((sizeof(Ts) <= sizeof(T0)) && ...)>
// { };
template <typename ...>
struct iftb;
template <typename T0>
struct iftb<T0> : public std::true_type
{ };
template <typename T0, typename T1, typename ... Ts>
struct iftb<T0, T1, Ts...>
: public std::integral_constant<bool,
(sizeof(T1) <= sizeof(T0)) && iftb<T0, Ts...>::value>
{ };
// ifctb = is first contained type bigger ?
template <typename>
struct ifctb;
template <template <typename ...> class C, typename ... Tc>
struct ifctb<C<Tc...>>
: public iftb<Tc...>
{ };
// to = type orderer
template <typename, typename Cd, bool = ifctb<Cd>::value>
struct to;
template <template <typename...> class C, typename ... To,
typename T0, typename ... Tu>
struct to<C<To...>, C<T0, Tu...>, true> : public to<C<To..., T0>, C<Tu...>>
{ };
template <template <typename...> class C, typename ... To,
typename T0, typename ... Tu>
struct to<C<To...>, C<T0, Tu...>, false> : public to<C<To...>, C<Tu..., T0>>
{ };
template <template <typename...> class C, typename ... To, typename T>
struct to<C<To...>, C<T>, true>
{ using type = C<To..., T>; };
template <std::size_t, typename>
struct wrapper;
template <typename T>
struct wrapper<0U, T>
{ T first; };
template <typename T>
struct wrapper<1U, T>
{ T second; };
template <typename T>
struct wrapper<2U, T>
{ T third; };
template <typename>
struct AlH2;
template <typename ... Ts>
struct AlH2<std::tuple<Ts...>> : public Ts...
{ };
template <typename...>
struct AlH1;
template <std::size_t ... Is, typename ... Ts>
struct AlH1<indexSequence<Is...>, Ts...>
: public AlH2<typename to<std::tuple<>,
std::tuple<wrapper<Is, Ts>...>>::type>
{ };
template <typename ... Ts>
struct Alignement
: public AlH1<makeIndexSequence<sizeof...(Ts)>, Ts...>
{ };
int main ()
{
Alignement<char, int, long long> a0;
a0.first = '0';
a0.second = 1;
a0.third = 2LL;
assert( (std::size_t)&a0.third < (std::size_t)&a0.first );
assert( (std::size_t)&a0.third < (std::size_t)&a0.second );
assert( (std::size_t)&a0.second < (std::size_t)&a0.first );
}
--编辑--
OP问
using your solution, if I want to achieve N-argument template class, I need to define N wrapper classes, each containing single field name for n-th argument. Different Alignement<>'s should have different field names == set of N wrappers for each of them. Any good idea for a macro (or template...) to achieve that?
对我来说,C 风格的宏是邪恶的(我不太了解它们),但是......
我提出的并不是一个完整的解决方案;只是草稿。
如果您定义以下一组宏
#define WrpNum(wName, num, fName) \
template <typename T>\
struct wrapper_ ## wName <num, T> \
{ T fName; };
#define Foo_1(wName, tot, fName) \
WrpNum(wName, tot-1U, fName)
#define Foo_2(wName, tot, fName, ...) \
WrpNum(wName, tot-2U, fName) \
Foo_1(wName, tot, __VA_ARGS__)
#define Foo_3(wName, tot, fName, ...) \
WrpNum(wName, tot-3U, fName) \
Foo_2(wName, tot, __VA_ARGS__)
// Foo_4(), Foo_5(), ...
#define Foo(wName, num, ...) \
template <std::size_t, typename> \
struct wrapper_ ## wName; \
Foo_ ## num(wName, num, __VA_ARGS__)
您可以定义索引为 struct
的模板wrapper_wrp1
具有特化和first
wrapper_wrp1<0U, T>
中的成员特化,a second
wrapper_wrp1<1U, T>
中的成员等,调用
Foo(wrp1, 3, first, second, third)
请注意,您需要特化的总数作为第二个参数。
也许可以做得更好(使用递归可变参数宏?),但是,坦率地说,我对宏不太感兴趣。
鉴于此电话
Foo(wrp1, 3, first, second, third)
您可以(注意:未经测试)修改AlH1
特定的包装结构( wrapper_wrp1
)
template <std::size_t ... Is, typename ... Ts>
struct AlH1<std::index_sequence<Is...>, Ts...>
: public AlH2<typename to<std::tuple<>,
std::tuple<wrapper_wrp1<Is, Ts>...>>::type>
{ };
关于c++ - 正确对齐内存中模板,参数顺序不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053107/