gpt4 book ai didi

c++ - 正确对齐内存模板,参数顺序不变

转载 作者:搜寻专家 更新时间:2023-10-31 01:30:23 25 4
gpt4 key购买 nike

看看这个模板。

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。 , secondthird (等等,如果你想扩展解决方案)

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_sequencestd::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_sequencestd::make_index_sequence indexSequencemakeIndexSequence (C++11 兼容的简化仿真 std::index_sequencestd::make_index_sequence ),我添加了几个助手 struct s 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>特化,一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com