C++ 结果、生成元组、参数包
我想获取给定参数包的C++ 结果、生成元组、参数包,c++,c++11,c++14,variadic-templates,template-meta-programming,C++,C++11,C++14,Variadic Templates,Template Meta Programming,我想获取给定参数包的std::make_tuple返回的类型。到目前为止,我已经编写了以下代码: #include <tuple> #include <functional> template <class T> struct unwrap_refwrapper { using type = T; }; template <class T> struct unwrap_refwrapper<std::reference_wrapp
std::make_tuple
返回的类型。到目前为止,我已经编写了以下代码:
#include <tuple>
#include <functional>
template <class T>
struct unwrap_refwrapper
{
using type = T;
};
template <class T>
struct unwrap_refwrapper<std::reference_wrapper<T>>
{
using type = T&;
};
template <class T>
using special_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type;
template<class ... Types>
struct foo
{
typedef std::tuple<special_decay_t<Types>...> tuple_t;
};
int main()
{
short s;
// t should be std::tuple<int, double&, short&>
typedef foo<int, double&, decltype(std::ref(s))>::tuple_t t;
}
但确实如此
怎么做?模板
template<class... Ts>
struct foo
{
using tuple_t = decltype(std::make_tuple(std::declval<Ts>()...));
};
结构foo
{
使用tuple_t=decltype(std::make_tuple(std::declval()…);
};
您应该给出一个可调用对象的result\u,但实际上您给出的是调用函数的结果。您可以使用decltype
代替它。为什么需要复杂的逻辑来对make\u tuple
的返回类型进行反向工程?它是指定的,并且不是超复杂的。@KerrekSB:我试图避免对make\u tuple
的返回类型进行反向工程,但迄今为止没有成功。但这正是我想要的。是的,是的,解决方案真的很好。
template<class... Ts>
struct foo
{
using tuple_t = decltype(std::make_tuple(std::declval<Ts>()...));
};