选择模板参数包中的每个偶数(或奇数)参数

发布于 2024-11-17 06:14:47 字数 814 浏览 1 评论 0原文

我想允许使用我正在编写的类来指定类型列表以及这些类型的分配器列表作为模板参数,其中类型位于奇数位置,分配器位于偶数位置:

template<typename... T>
class MyClass {
  // Stuff inside
}

int main() {
  MyClass<SomeType1, AllocatorOfSomeType1> c1;
  MyClass<SomeType1, AllocatorOfSomeType1, 
          SomeType2, AllocatorOfSomeType2> c2;
  MyClass<SomeType1, AllocatorOfSomeType1, 
          SomeType2, AllocatorOfSomeType2,
          SomeType3, AllocatorOfSomeType3> c3;
  // And so on....
}

在内部 是有意义的

std::tuple<std::vector<EveryOddTypeInParameterPack>...> m_storage_;

拥有一个用于存储的类型向量元组和一个用于使用的分配器元组

std::tuple<std::vector<EveryEvenTypeInParameterPack>...> m_storage_;

:我如何在代码中实际声明这些元组?理论上,我需要以某种方式选择参数包中的每个奇数/偶数类型 - 这可能吗?

I would like to allow use of the class I'm writing to specify as a template parameters a list of types along with a list of allocators of those types in a manner that types are at odd positions and allocators are at even ones:

template<typename... T>
class MyClass {
  // Stuff inside
}

int main() {
  MyClass<SomeType1, AllocatorOfSomeType1> c1;
  MyClass<SomeType1, AllocatorOfSomeType1, 
          SomeType2, AllocatorOfSomeType2> c2;
  MyClass<SomeType1, AllocatorOfSomeType1, 
          SomeType2, AllocatorOfSomeType2,
          SomeType3, AllocatorOfSomeType3> c3;
  // And so on....
}

Internally it would make sense to have a tuple of vectors of types for storage:

std::tuple<std::vector<EveryOddTypeInParameterPack>...> m_storage_;

and a tuple of allocators for usage:

std::tuple<std::vector<EveryEvenTypeInParameterPack>...> m_storage_;

How can I actually declare those tuples in code? In theory I need to somehow select every odd/even type in parameter pack - is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不交电费瞎发啥光 2024-11-24 06:14:47

虽然代码有点长,但我想该机制没有
不必要的特性。
如果我正确理解了这个问题,
可能下面的代码可以满足目的:

// push front for tuple
template< class, class > struct PFT;

template< class A, class... T > struct PFT< A, tuple< T... > > {
  typedef tuple< A, T... > type;
};

// for even
template< class... > struct even_tuple;

template< class A, class B > struct even_tuple< A, B > {
  typedef tuple< A > type;
};
template< class A, class B, class... T > struct even_tuple< A, B, T... > {
  typedef typename PFT< A, typename even_tuple< T... >::type >::type type;
};
// As for odd elements, in the same way as even(please see the test on ideone)

// objective type
template< class > struct storage_type;

template< class... T > struct storage_type< tuple< T... > > {
  typedef tuple< vector< T >... > type;
};

template< class... T >
struct MyClass {
  typename storage_type< typename even_tuple< T... >::type >::type
    m_storage_even_;
  typename storage_type< typename  odd_tuple< T... >::type >::type
    m_storage_odd_;
};

这是对 ideone 的测试。

Though the code got a little lengthy, I suppose the mechanism doesn't have
unnecessary peculiarities.
If I understand the question correctly,
probably the following code will meet the purpose:

// push front for tuple
template< class, class > struct PFT;

template< class A, class... T > struct PFT< A, tuple< T... > > {
  typedef tuple< A, T... > type;
};

// for even
template< class... > struct even_tuple;

template< class A, class B > struct even_tuple< A, B > {
  typedef tuple< A > type;
};
template< class A, class B, class... T > struct even_tuple< A, B, T... > {
  typedef typename PFT< A, typename even_tuple< T... >::type >::type type;
};
// As for odd elements, in the same way as even(please see the test on ideone)

// objective type
template< class > struct storage_type;

template< class... T > struct storage_type< tuple< T... > > {
  typedef tuple< vector< T >... > type;
};

template< class... T >
struct MyClass {
  typename storage_type< typename even_tuple< T... >::type >::type
    m_storage_even_;
  typename storage_type< typename  odd_tuple< T... >::type >::type
    m_storage_odd_;
};

Here is a test on ideone.

极度宠爱 2024-11-24 06:14:47

也许是这样的:

#include <tuple>

// Example receptacle    
template <typename ...Args> struct MyContainer;

// Tuple concatenator
template<typename PackR, typename PackL> struct cat;
template<typename ...R, typename ...L>
struct cat<std::tuple<R...>, std::tuple<L...>>
{
  typedef std::tuple<R..., L...> type;
};

// Even/Odd extractors
template <typename ...Args> struct GetEven;
template <typename ...Args> struct GetOdd;

template <typename E1, typename O1, typename ...Args>
struct GetEven<E1, O1, Args...>
{
  typedef typename cat<std::tuple<E1>, typename GetEven<Args...>::value>::type value;
};
template <typename E1, typename O1>
struct GetEven<E1, O1>
{
  typedef std::tuple<E1> value;
};

template <typename E1, typename O1, typename ...Args>
struct GetOdd<E1, O1, Args...>
{
  typedef typename cat<std::tuple<O1>, typename GetEven<Args...>::value>::type value;
};
template <typename E1, typename O1>
struct GetOdd<E1, O1>
{
  typedef std::tuple<O1> value;
};

// Tuple-to-Receptacle mover
template <typename Pack, template <typename ...T> class Receiver> struct Unpack;
template <typename ...Args, template <typename ...T> class Receiver>
struct Unpack<std::tuple<Args...>, Receiver>
{
  typedef Receiver<Args...> type;
};

// Example consumer
template <typename ...Args>
struct Foo
{
  typedef typename Unpack<typename GetEven<Args...>::value, MyContainer>::type EvenVector;
  typedef typename Unpack<typename GetOdd<Args...>::value, MyContainer>::type OddVector;

  EvenVector x;
  OddVector y;
};

您仍然需要定义您的 MyContainer 类来对可变参数执行一些有用的操作,例如实现您的向量元组...(但为什么不是元组向量?

)到 brunocodutra 用于元组技巧。

Perhaps something like this:

#include <tuple>

// Example receptacle    
template <typename ...Args> struct MyContainer;

// Tuple concatenator
template<typename PackR, typename PackL> struct cat;
template<typename ...R, typename ...L>
struct cat<std::tuple<R...>, std::tuple<L...>>
{
  typedef std::tuple<R..., L...> type;
};

// Even/Odd extractors
template <typename ...Args> struct GetEven;
template <typename ...Args> struct GetOdd;

template <typename E1, typename O1, typename ...Args>
struct GetEven<E1, O1, Args...>
{
  typedef typename cat<std::tuple<E1>, typename GetEven<Args...>::value>::type value;
};
template <typename E1, typename O1>
struct GetEven<E1, O1>
{
  typedef std::tuple<E1> value;
};

template <typename E1, typename O1, typename ...Args>
struct GetOdd<E1, O1, Args...>
{
  typedef typename cat<std::tuple<O1>, typename GetEven<Args...>::value>::type value;
};
template <typename E1, typename O1>
struct GetOdd<E1, O1>
{
  typedef std::tuple<O1> value;
};

// Tuple-to-Receptacle mover
template <typename Pack, template <typename ...T> class Receiver> struct Unpack;
template <typename ...Args, template <typename ...T> class Receiver>
struct Unpack<std::tuple<Args...>, Receiver>
{
  typedef Receiver<Args...> type;
};

// Example consumer
template <typename ...Args>
struct Foo
{
  typedef typename Unpack<typename GetEven<Args...>::value, MyContainer>::type EvenVector;
  typedef typename Unpack<typename GetOdd<Args...>::value, MyContainer>::type OddVector;

  EvenVector x;
  OddVector y;
};

You still have to define your MyContainer class to do something useful with the variadic parameters, e.g. implement your tuple of vectors... (why not a vector of tuples, though?)

Credits to brunocodutra for the tuple trick.

贩梦商人 2024-11-24 06:14:47

这只是一次尝试,

template<typename... T> class Myclass;

template<typename T1, typename allocT1>
class MyClass <T1, allocT1> {
  std::pair<T1, allocT1> myFirstArglist;
//and you have to do a check that allocT1::value_type is same as T1 or not
//or may be alloT1 is an allocator type or not(i'm thinking concepts, may be)
//this idea is inspired from Chris's comment
};

template<typename T1, typename allocT1, typename... T>
class Myclass<T1, allocT1, T...> {
std::pair<T1, allocT1> myFirstArglist;
Myclass<T>; //something like this
};

template<>
class Myclass<> {
//probably you would like some error message here
//when there are no types and containers
};

可能我还不够清楚,你可能想阅读
http://www.open-std。 org/jtc1/sc22/wg21/docs/papers/2006/n2080.pdf

还有一篇与分配器设计相关的好帖子类型...您想看看:
分配器类型参数的 C++ 设计模式

this is just a try

template<typename... T> class Myclass;

template<typename T1, typename allocT1>
class MyClass <T1, allocT1> {
  std::pair<T1, allocT1> myFirstArglist;
//and you have to do a check that allocT1::value_type is same as T1 or not
//or may be alloT1 is an allocator type or not(i'm thinking concepts, may be)
//this idea is inspired from Chris's comment
};

template<typename T1, typename allocT1, typename... T>
class Myclass<T1, allocT1, T...> {
std::pair<T1, allocT1> myFirstArglist;
Myclass<T>; //something like this
};

template<>
class Myclass<> {
//probably you would like some error message here
//when there are no types and containers
};

may be i'm not clear enough, you'd probably like to read
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2080.pdf

Also there is a good post related to design of allocator types... you would like to have a look at:
C++ Design Pattern for allocator type arguments

绿萝 2024-11-24 06:14:47

我知道你的问题最初被标记为“c++11”,但我认为值得向后代指出,在 C++14 中你可以访问 make_index_sequence,这使得整个事情变得非常简单。为了过滤元组,我将从以下大纲开始: https:// quuxplusone.github.io/blog/2018/07/23/metafilter/

然后我们最终得到这样的结果(Godbolt):

template<bool> struct zero_or_one {
    template<class E> using type = std::tuple<E>;
};

template<> struct zero_or_one<false> {
    template<class E> using type = std::tuple<>;
};

template<class Tuple, class = std::make_index_sequence<std::tuple_size<Tuple>::value>>
struct just_evens;

template<class... Es, size_t... Is>
struct just_evens<std::tuple<Es...>, std::index_sequence<Is...>> {
    using type = decltype(std::tuple_cat(
        std::declval<typename zero_or_one<Is % 2 == 0>::template type<Es>>()...
    ));
};

要获得 just_odds,您需要从 切换条件是 % 2 == 0是 % 2 != 0

用法示例:

static_assert(std::is_same<
    just_evens<std::tuple<char, short, int, long, double>>::type,
    std::tuple<char, int, double>
>::value, "");

I know your question was originally tagged "c++11", but I figure it's worth pointing out for posterity that in C++14 you have access to make_index_sequence, and that makes the whole thing pretty simple. For filtering a tuple, I'd start with this outline: https://quuxplusone.github.io/blog/2018/07/23/metafilter/

And then we end up with something like this (Godbolt):

template<bool> struct zero_or_one {
    template<class E> using type = std::tuple<E>;
};

template<> struct zero_or_one<false> {
    template<class E> using type = std::tuple<>;
};

template<class Tuple, class = std::make_index_sequence<std::tuple_size<Tuple>::value>>
struct just_evens;

template<class... Es, size_t... Is>
struct just_evens<std::tuple<Es...>, std::index_sequence<Is...>> {
    using type = decltype(std::tuple_cat(
        std::declval<typename zero_or_one<Is % 2 == 0>::template type<Es>>()...
    ));
};

To get just_odds, you'd switch the condition from Is % 2 == 0 to Is % 2 != 0.

Example usage:

static_assert(std::is_same<
    just_evens<std::tuple<char, short, int, long, double>>::type,
    std::tuple<char, int, double>
>::value, "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文