[英]get concatenated tuple type; combine result_of and tuple_cat
我想从我的函数返回std::tuple_cat
的结果,但是我无法推断出返回类型
#include <tuple>
struct H {
typedef std::tuple<int,int> tuple_type;
tuple_type a {1,2};
};
template <typename tuple_holder_type, typename B>
???
func(tuple_holder_type h, B b) {
return std::tuple_cat(h.a,std::make_tuple(b));
}
int main(int argc, char const *argv[]) {
auto h = H();
auto b = 3;
auto c = func(h,b);
return 0;
}
我试图像这样结合std::result_of
和std::tuple_cat
typename std::result_of<std::tuple_cat(tuple_holder_type::tuple_type,std::tuple<B>) >::type
但只有错误消息
test.cpp:9:85: error: template argument 1 is invalid
test.cpp:9:86: error: expected identifier before '::' token
test.cpp:10:1: error: expected initializer before 'func'
问题:为了解决这个问题,我要问些什么而不是问号
奖金q:为什么有效
编辑忘了提到我需要这样一种方式,我可以将结果类型放入typedef
,从而得到类似
template <typename tuple_holder_type, typename B>
struct tuple_appender {
typedef ??? return_type;
return_type operator() /*...*/
}
谢谢 :)
在C ++ 11中,您可以像这样使用decltype
:
template <typename tuple_holder_type, typename B>
auto
func(tuple_holder_type h, B b)
-> decltype(std::tuple_cat(h.a,std::make_tuple(b)))
{
return std::tuple_cat(h.a,std::make_tuple(b));
}
在C ++ 1y工作草案中,您可以像这样删除decltype
:
template <typename tuple_holder_type, typename B>
auto
func(tuple_holder_type h, B b)
{
return std::tuple_cat(h.a,std::make_tuple(b));
}
这是如何获得func
的返回类型并将其放入typedef
,无论func
的返回类型如何编码:
template <typename tuple_holder_type, typename B>
struct tuple_appender {
typedef decltype(func(std::declval<typename tuple_holder_type::tuple_type>(),
std::declval<std::tuple<B>>())) return_type;
};
std::declval<T>()
只是一种获取类型T
的右值表达式的方法,而不必像T()
那样默认构造一个。 您可能不希望T
是默认可构造的。 您还可以使用declval<T&>()
获得T
的左值表达式,或者使用declval<const T&>()
获得const左值表达式,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.