使用C ++元组类型作为映射中的键

时间:2014-10-02 14:16:59

标签: c++ loops tuples sfinae

我正在尝试将不同类型的值集合映射到连续空间。要做到这一点(有效地),我需要知道所有元素的总大小和每个元素的偏移量。如果将集合作为元组给出,则获得总大小很容易。它还允许预先计算偏移量。我很难通过类型获得偏移量。为了简化示例,我假设元组中的类型将是唯一的(实际上,元组将具有唯一标记对+非唯一值)。这是我目前的未编译尝试:

#include <cstddef>
#include <iostream>
#include <tuple>

struct A
{
    std::size_t size()
    {
        return 3;
    }
};

struct B
{
    std::size_t size()
    {
        return 2;
    }
};

struct C
{
    std::size_t size()
    {
        return 4;
    }
};

template <typename Tuple>
struct Foo
{
    const Tuple& tuple_;
    std::array<int, std::tuple_size<Tuple>::value> array_;

    Foo(const Tuple& tuple) : tuple_(tuple)
    {
        std::cout << init() << '\n';
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    template <std::size_t INDEX = 0>
    typename std::enable_if<std::tuple_size<Tuple>::value == INDEX, std::size_t>::type
    init()
    {
        return 0;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    template <std::size_t INDEX = 0>
    typename std::enable_if<std::tuple_size<Tuple>::value != INDEX, std::size_t>::type
    init()
    {
        auto offset = init<INDEX + 1>();

        std::cout << "index: "<< INDEX << "; offset: " << offset << '\n';

        array_[INDEX] = offset;

        return offset + std::get<INDEX>(tuple_).size();
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    template <std::size_t INDEX = 0, typename T>
    typename std::enable_if<std::tuple_size<Tuple>::value == INDEX, std::size_t>::type
    offset(const T&)
    {
        return 0;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    template <std::size_t INDEX = 0, typename T>
    typename std::enable_if<(std::tuple_size<Tuple>::value != INDEX) && !std::is_same<typename std::tuple_element<INDEX, Tuple>::type, T>::value, std::size_t>::type
    offset(const T& t)
    {
        return offset<INDEX + 1>(t);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    template <std::size_t INDEX = 0, typename T>
    typename std::enable_if<(std::tuple_size<Tuple>::value != INDEX) && std::is_same<typename std::tuple_element<INDEX, Tuple>::type, T>::value, std::size_t>::type
    offset(const T&)
    {
        return array_[INDEX];
    }    
};

int main()
{
    A a;
    B b;
    C c;
    auto t = std::tie(a, b, c);
    using T = decltype(t);
    Foo<T> foo(t);

    std::cout << foo.offset(a) << '\n';
    //std::cout << foo.offset(b) << '\n';
    //std::cout << foo.offset(c) << '\n';
}

编译错误:

In file included from prog.cpp:3:0:
/usr/include/c++/4.8/tuple: In instantiation of ‘struct std::tuple_element<1u, std::tuple<C&> >’:
/usr/include/c++/4.8/tuple:680:12:   recursively required from ‘struct std::tuple_element<2u, std::tuple<B&, C&> >’
/usr/include/c++/4.8/tuple:680:12:   required from ‘struct std::tuple_element<3u, std::tuple<A&, B&, C&> >’
prog.cpp:79:22:   recursively required from ‘typename std::enable_if<((std::tuple_size<_Tp>::value != INDEX) && (! std::is_same<typename std::tuple_element<INDEX, Tuple>::type, T>::value)), unsigned int>::type Foo<Tuple>::offset(const T&) [with unsigned int INDEX = 1u; T = A; Tuple = std::tuple<A&, B&, C&>; typename std::enable_if<((std::tuple_size<_Tp>::value != INDEX) && (! std::is_same<typename std::tuple_element<INDEX, Tuple>::type, T>::value)), unsigned int>::type = unsigned int]’
prog.cpp:79:22:   required from ‘typename std::enable_if<((std::tuple_size<_Tp>::value != INDEX) && (! std::is_same<typename std::tuple_element<INDEX, Tuple>::type, T>::value)), unsigned int>::type Foo<Tuple>::offset(const T&) [with unsigned int INDEX = 0u; T = A; Tuple = std::tuple<A&, B&, C&>; typename std::enable_if<((std::tuple_size<_Tp>::value != INDEX) && (! std::is_same<typename std::tuple_element<INDEX, Tuple>::type, T>::value)), unsigned int>::type = unsigned int]’
prog.cpp:101:27:   required from here
/usr/include/c++/4.8/tuple:680:12: error: invalid use of incomplete type ‘struct std::tuple_element<0u, std::tuple<> >’
     struct tuple_element<__i, tuple<_Head, _Tail...> >
            ^
In file included from /usr/include/c++/4.8/tuple:38:0,
                 from prog.cpp:3:
/usr/include/c++/4.8/utility:84:11: error: declaration of ‘struct std::tuple_element<0u, std::tuple<> >’
     class tuple_element;
           ^

2 个答案:

答案 0 :(得分:2)

除一个问题外,您的代码是正确的。

问题是您使用std::tuple辅助方法构建std::tie(),这会产生 引用元组 - {{1} }。

另一方面,您有std::tuple<A&, B&, C&>,其中推断的类型offset(const T& t)可以只是TAB(即引用不是推导类型的一部分,只是参数的类型)。也就是说,您的条件C 总是失败,因为一种类型是引用,而另一种类型不是,例如is_same<tuple_element<...>::type, T>。要解决这个问题,首先应该衰减类型(或至少删除引用),然后再将其与另一个进行比较,以确保相等。

话虽如此,您的每一个条件应如下所示:

std::is_same<A&, A>

或(C ++ 14):

std::is_same<typename std::decay<typename std::tuple_element<INDEX, Tuple>::type>::type, T>::value
//           ~~~~~~~~~~~~~~~~~~~^                                               ~~~~~~^

DEMO

答案 1 :(得分:1)

似乎你想要这样的东西:

template <typename T, typename Tuple> struct get_index;

template <typename T, typename... Ts>
struct get_index<T, std::tuple<T, Ts...>> : std::integral_constant<std::size_t, 0> {};

template <typename T, typename Tail, typename... Ts>
struct get_index<T, std::tuple<Tail, Ts...>> :
    std::integral_constant<std::size_t, 1 + get_index<T, std::tuple<Ts...>>::value> {};

然后用

替换所有的偏移方法
template <typename T>
std::size_t
offset(const T&)
{
    return array_[get_index<T&, Tuple>::value];
}

Live example

请注意,示例Tuple中的std::tuple<A&, B&, C&>A并且您使用{{1}}测试是否相等......