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

#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;
           ^

最佳答案

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

问题是您使用std::tuple helper函数构造了std::tie(),这导致引用元组-std::tuple<A&, B&, C&>

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

话虽如此,您的每种情况应如下所示:

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

或在c++14中:
std::is_same<std::decay_t<typename std::tuple_element<INDEX, Tuple>::type>::type>, T>::value
//           ~~~~~~~~~~~~^                                                      ^

DEMO

关于c++ - 使用C++元组类型作为映射中的键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26162836/

10-11 02:37
查看更多