template <size_t N>
struct tuple_helper_get {
template <typename T, typename... Args> constexpr static
auto get_ref(tuple_helper<T, Args...> &t) noexcept
-> decltype(tuple_helper_get<N - 1>::get_ref
(static_cast<tuple_helper<Args...> &>(t)))
{
return tuple_helper_get<N - 1>::get_ref(
static_cast<tuple_helper<Args...> &>(t)); //!required from ‘ static constexpr decltype (glgltinySTL::tuple_helper_get<(N - 1)>::get_ref(static_cast<glgltinySTL::tuple_helper<Args ...>&>(t))) glgltinySTL::tuple_helper_get<N>::get_ref(glgltinySTL::tuple_helper<Arg, Args ...>&) [with T = int; Args = {char, unsigned int}; long unsigned int N = 2ul; decltype (glgltinySTL::tuple_helper_get<(N - 1)>::get_ref(static_cast<glgltinySTL::tuple_helper<Args ...>&>(t))) = unsigned int&]’
}
//...
};
template <>
struct tuple_helper_get<1> {
template <typename T1, typename T2> constexpr static
T2 &get_ref(tuple_helper<T1, T2> &t) noexcept
{
return (t.t2); //!错误:用类型为‘ unsigned int ’的右值初始化类型为‘ unsigned int&’的非常量引用无效
} //!错误: body of constexpr function ‘ static constexpr T2& glgltinySTL::tuple_helper_get<1ul>::get_ref(glgltinySTL::tuple_helper<T1, T2>&) [with T1 = char; T2 = unsigned int]’ not a return-statement
//...
};
template <size_t N, typename... Args> constexpr
auto get(tuple<Args...> &t) noexcept
-> decltype(tuple_helper_get<N>::get_ref
(static_cast<tuple_helper<Args...> &>(t)))
{
return tuple_helper_get<N>::get_ref(
static_cast<tuple_helper<Args...> &>(t)); //!required from ‘ constexpr decltype (glgltinySTL::tuple_helper_get<N>::get_ref(static_cast<glgltinySTL::tuple_helper<OtherArgs ...>&>(t))) glgltinySTL::get(glgltinySTL::tuple<Args ...>&) [with long unsigned int N = 2ul; Args = {int, char, unsigned int}; decltype (glgltinySTL::tuple_helper_get<N>::get_ref(static_cast<glgltinySTL::tuple_helper<OtherArgs ...>&>(t))) = unsigned int&]’
}
void test_tuple()
{
glgltinySTL::pair<int, unsigned> p{ -1, 1 };
glgltinySTL::tuple<int, char, unsigned> t{ -1, 'a', 1 };
std::cout << glgltinySTL::get<0>(t) << std::endl;
std::cout << glgltinySTL::get<1>(t) << std::endl;
std::cout << glgltinySTL::get<2>(t) << std::endl; //!required from here
std::cout << glgltinySTL::get<0>(p) << std::endl;
std::cout << glgltinySTL::get<1>(p) << std::endl;
}
明明每个函数都传递的引用,怎么变成右值了?而且,注意到其他几个都没事,可能是
template <size_t N>
struct tuple_helper_get {//...};的问题。。。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.