c++ - 如何正确传播通用引用的类型?

标签 c++ perfect-forwarding universal-reference

我的代码库中有一种情况,我必须实现一种通用形式的 std::get(),它适用于任何类型的类似元组的类型。该函数接受对 Tuple 的通用引用,并返回对 Tuple 的第 I 元素的引用。我不知道如何命名引用的类型。不幸的是,我无法使用 auto 返回类型,只能让编译器自行解决。

这是我的第一次尝试:

#include <type_traits>
#include <tuple>

template<class T, class U>
struct propagate_reference
{
  using type = U;
};

template<class T, class U>
struct propagate_reference<T&,U>
{
  using type = typename std::add_lvalue_reference<U>::type;
};

template<class T, class U>
struct propagate_reference<T&&,U>
{
  using type = typename std::add_rvalue_reference<U>::type;
};

template<size_t I, class TupleReference>
struct get_result
{
  using tuple_type = typename std::decay<TupleReference>::type;

  using type = typename propagate_reference<
    TupleReference,
    typename std::tuple_element<I,tuple_type>::type
  >::type;
};

template<size_t I, class Tuple>
typename get_result<I,Tuple&&>::type my_get(Tuple&& t)
{
  return std::get<I>(std::forward<Tuple>(t));
}

int foo(const std::tuple<int>& t)
{
  return my_get<0>(t);
}

int main()
{
  return 0;
}

Clang 拒绝这个程序:

$ clang -std=c++11 test_get.cpp 
test_get.cpp:36:10: error: binding of reference to type 'int' to a value of type 'const __tuple_element_t<0UL, tuple<int> >' (aka 'const int') drops qualifiers
  return std::get<I>(std::forward<Tuple>(t));
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_get.cpp:41:10: note: in instantiation of function template specialization 'my_get<0, const std::tuple<int> &>' requested here
  return my_get<0>(t);
         ^
1 error generated.

我怀疑问题出在我实例化 get_result 的方式上。我做错了什么?

最佳答案

问题是 std::decay删除 cv 限定符,并将结果类型定义为成员 typedef 类型。正如 TC 在评论中提到的那样,您在这里需要的是 std::remove_reference :

template<size_t I, class TupleReference>
struct get_result
{
  using tuple_type = typename std::remove_reference<TupleReference>::type;

  using type = typename propagate_reference<
    TupleReference,
    typename std::tuple_element<I,tuple_type>::type
  >::type;
};

Live Demo

关于c++ - 如何正确传播通用引用的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34142048/

相关文章:

c++ - 从 shared_ptr 到 weak_ptr 多态性的转换

c++ - 为什么我不能从一对中返回一个 unique_ptr?

c++ - 编译器如何知道必须调用 std::forward 函数的哪个重载?

c++ - 放置和默认构造函数

c++ - 无法将左值绑定(bind)到 A<Cv2>&&

c++ - 线程数之间的事件c++

c++ - C++ 类中的返回函数指针

c++ - VS 2013 无法根据模板参数专门化具有通用引用和返回类型的函数模板

c++ - C++ 中的多态性从不向上转型

c++ - 将函数引用作为通用引用传递