Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;包含多个包的模板,按参数和返回值进行扣减 我有点困惑C++如何解释下面的代码: template<class ... ReturnTypes, class ... ParameterTypes> std::tuple<ReturnTypes...> Method(const ParameterTypes & ... Parameters) { (...) }; std::tuple<unsigned int> R = Object.Method<unsigned int, unsigned int>(10); 模板 std::tuple方法(常量参数类型和参数) { (...) };_C++_Templates_C++11 - Fatal编程技术网

c++;包含多个包的模板,按参数和返回值进行扣减 我有点困惑C++如何解释下面的代码: template<class ... ReturnTypes, class ... ParameterTypes> std::tuple<ReturnTypes...> Method(const ParameterTypes & ... Parameters) { (...) }; std::tuple<unsigned int> R = Object.Method<unsigned int, unsigned int>(10); 模板 std::tuple方法(常量参数类型和参数) { (...) };

c++;包含多个包的模板,按参数和返回值进行扣减 我有点困惑C++如何解释下面的代码: template<class ... ReturnTypes, class ... ParameterTypes> std::tuple<ReturnTypes...> Method(const ParameterTypes & ... Parameters) { (...) }; std::tuple<unsigned int> R = Object.Method<unsigned int, unsigned int>(10); 模板 std::tuple方法(常量参数类型和参数) { (...) };,c++,templates,c++11,C++,Templates,C++11,在编译以下代码时: template<class ... ReturnTypes, class ... ParameterTypes> std::tuple<ReturnTypes...> Method(const ParameterTypes & ... Parameters) { (...) }; std::tuple<unsigned int> R = Object.Method<unsigned int, unsigned in

在编译以下代码时:

template<class ... ReturnTypes, class ... ParameterTypes>
std::tuple<ReturnTypes...> Method(const ParameterTypes & ... Parameters)
{
    (...)
};
std::tuple<unsigned int> R = Object.Method<unsigned int, unsigned int>(10);
std::tuple R=Object.Method(10);
我得到:

error: conversion from 'std::tuple<unsigned int, unsigned int>' to non-scalar type 'std::tuple<unsigned int>' requested
  std::tuple<unsigned int> R = Object.Method<unsigned int, unsigned int>(10);
错误:请求从'std::tuple'转换为非标量类型'std::tuple'
std::tuple R=Object.Method(10);

是否可以创建一个模板方法(在非模板类中),该方法有两个参数包-一个用于返回类型(元组中),另一个用于参数类型?

您可以使用模板参数来区分这两个参数包:

#include <tuple>

template <class... T> struct typelist { };

template <typename... ReturnTypes,
          template <typename...> class T,
          class ... ParameterTypes>
std::tuple<ReturnTypes...> Method(T<ReturnTypes...>,
                                  const ParameterTypes & ... Parameters)
{
    return std::tuple<ReturnTypes...>();
};

int main()
{
    using ReturnTypes = typelist<int, char>;
    auto t = Method(ReturnTypes{}, 10, "hello", false);

    static_assert(std::is_same<decltype(t), std::tuple<int,char>>{}, "types do not match");
    return 0;
}
#包括
模板结构类型列表{};
模板
std::元组方法(T,
常量参数类型(&…参数)
{
返回std::tuple();
};
int main()
{
使用ReturnTypes=typelist;
auto t=Method(ReturnTypes{},10,“hello”,false);
静态_断言(std::is_same{},“类型不匹配”);
返回0;
}

此解决方案利用了C++14的功能,但提供了所需的语法:

#include <utility>
#include <tuple>
#include <cstddef>

template <typename...> struct typelist {};
template <typename T> struct identity { using type = T; };

template <class... Ts, class... Us>
auto Method3(typelist<Ts...>, typelist<Us...>, const typename identity<Us>::type&... parameters)
    -> std::tuple<Ts...>
{
    // (...)
    return {};
}

template <class... Ts, class... Params, std::size_t... Is, std::size_t... Js>
auto Method2(std::index_sequence<Is...>, std::index_sequence<Js...>, Params&&... parameters)
    -> decltype(auto)
{
    return Method3(typelist<typename std::tuple_element<Is, std::tuple<Ts...>>::type...>{}
                 , typelist<typename std::tuple_element<sizeof...(Is) + Js, std::tuple<Ts...>>::type...>{}
                 , std::forward<Params>(parameters)...);
}

template <class... Ts, class... Params>
auto Method(Params&&... parameters)
    -> decltype(auto)
{
    return Method2<Ts...>(std::make_index_sequence<sizeof...(Ts) - sizeof...(Params)>{}
                        , std::make_index_sequence<sizeof...(Params)>{}
                        , std::forward<Params>(parameters)...);
}

int main()
{
    std::tuple<int, short, float> R = Method<int, short, float, double, bool>(3.14, true);
}
#包括
#包括
#包括
模板结构类型列表{};
模板结构标识{using type=T;};
模板
自动方法3(类型列表、类型列表、常量类型名称标识::类型和参数)
->std::tuple
{
// (...)
返回{};
}
模板
自动方法2(std::index_序列、std::index_序列、参数和参数)
->decltype(自动)
{
返回方法3(类型列表{}
,类型列表{}
,std::forward(参数)…);
}
模板
自动方法(参数&&…参数)
->decltype(自动)
{
返回方法2(std::make_index_sequence{}
,std::make_index_sequence{}
,std::forward(参数)…);
}
int main()
{
std::tuple R=方法(3.14,真);
}

一种解决方案是使用一个伪类来分隔类型,使用一个助手类来获得正确的
方法

template<typename...> class type_pack;

template<typename...> class MethodHelper;

template<typename ... ReturnTypes, typename ... ArgTypes>
class MethodHelper<type_pack<ReturnTypes...>, type_pack<ArgTypes...>>{
    public:
        static_assert(
            sizeof...(ReturnTypes) == sizeof...(ArgTypes),
            "number of return types not the same as argument types"
        );

        static auto Method(ArgTypes ... args){
            return std::tuple<ReturnTypes...>(args...);
        }
};

template<typename ReturnPack, typename ArgsPack, typename ... Args>
auto Method(Args &&... args){
    return MethodHelper<ReturnPack, ArgsPack>::Method(std::forward<Args>(args)...);
}
模板类类型\u包;
模板类MethodHelper;
模板
类MethodHelper{
公众:
静态断言(
sizeof…(ReturnTypes)==sizeof…(ArgTypes),
“返回类型的数量与参数类型不同”
);
静态自动方法(ArgTypes…args){
返回std::tuple(args…);
}
};
模板
自动方法(Args&&…Args){
returnmethodhelper::Method(std::forward(args)…);
}
然后您可以像最初一样使用它:

auto main() -> int{
    auto t = Method<type_pack<int, int>, type_pack<size_t, size_t>>(5, 6);
}
auto main()->int{
自动t=方法(5,6);
}
或者使用别名让每个人的生活更轻松:

auto main() -> int{
    static constexpr auto &&my_method = Method<type_pack<int, int>, type_pack<float, float>>::Method;

    auto my_tuple = my_method(1.234f, 5.678f);
}
auto main()->int{
静态constexpr auto&&my_method=method::method;
自动my_元组=my_方法(1.234f,5.678f);
}

我不喜欢您选择的语法,因为不太清楚哪些参数与函数参数对齐,哪些与返回值对齐

我可以看到三种方法

首先,保留您的语法:

template<class...TypeParams, class...Args>
auto Method( Args&&... args )
-> // magic
这里我们调用
方法
,如下所示:

auto result = Method( types<double>, types<double>, 3 );
自动结果=方法(类型,类型,3);

其中,我们将两个类型束作为显式
type\u tw传递,您为什么要显式指定参数的类型?你可以使用
Object.Method(10)甚至编写
模板std::tuple方法(const参数类型和参数)
Instead至少对参数大小具有基本控制权Return类型并不总是与参数类型相同。此方法用于从数据库中选择数据,返回类型用于列,参数类型用于参数绑定A有助于了解您的用法case@peku33你的<代码>使用< /COD>语句用于<代码>类型无效C++。您正在尝试执行C++14模板变量还是类型别名?
std::tuple
不会展开
ReturnTypes
。你可能是指
std::tuple
和你的代码@谢谢你指出了这一点(y)。你能解释一下为什么它不适用于右值吗?我很难理解为什么。如果我对
常量ArgTypes&…
进行重载,它当然对左值有效,但是每个参数都会转换为
常量&
。因为
ArgTypes
不是函数模板参数,
ArgTypes&&
不会用作转发引用。它是固定的,并且与您在
type\u pack
中传递的内容相同,例如
type\u pack
使您的函数具有固定的签名
方法(float&&,float&&)
,这不允许您绑定lvalues@PiotrSkotnicki当然谢谢,我会找到答案的
auto result = Method<double>( std::tuple<double>(3) );