Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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++ 分配lside上的函数问题_C++_Function_Templates - Fatal编程技术网

C++ 分配lside上的函数问题

C++ 分配lside上的函数问题,c++,function,templates,C++,Function,Templates,我在测试用例中定义了这个元组 std::tuple<int, string, string, double> t1 = { 1321, "xxxx", "xxxx", 1.68 } 我不能更改上面的定义,因为它们在测试用例中,我不能修改它 我知道函数应该这样声明: &unpack() 我应该使用模板,但我不知道如何使用。感谢您的回答。如果您可以访问C++17,则可以使用结构化绑定: #include <tuple> #i

我在测试用例中定义了这个元组

std::tuple<int, string, string, double> t1 = { 1321, "xxxx", "xxxx", 1.68 }
我不能更改上面的定义,因为它们在测试用例中,我不能修改它

我知道函数应该这样声明:

&unpack()

我应该使用模板,但我不知道如何使用。感谢您的回答。

如果您可以访问
C++17
,则可以使用结构化绑定:

#include <tuple>
#include <string>

std::tuple<int, string, string, double> tpl = { 1321, "xxxx", "xxxx", 1.68 };

const auto& [a,b,c,d] = tpl;
语法很笨拙,因为您必须事先定义变量,因此无法使它们
const


我必须编写这个函数,因为元组和赋值的定义在测试用例中,我不能修改它

模板
constexpr decltype(自动)解包(Args&&…Args)无异常
{
返回std::tie(std::forward(args)…);
}

这已经存在:听起来不太像,但再加上一个简单的例子,我想你有一个答案,@tkausldo你需要编写函数
解包
还是需要一个可以用你描述的方式使用的函数?我不能使用std::tie,因为在测试用例中我的函数不应该通过。因此,我必须编写函数解包,但我不知道如何做。如果您在收到答案后更改问题,我建议您明确更改内容。我必须编写函数,因为元组和赋值的定义在测试用例中,我无法修改。
#include <tuple>
#include <string>

std::tuple<int, string, string, double> tpl = { 1321, "xxxx", "xxxx", 1.68 };

const auto& [a,b,c,d] = tpl;
...
int a;
string b,c;
double d;

std::tie(a,b,c,d) = tpl;
template <class... Args>
constexpr decltype(auto) unpack(Args&&... args) noexcept
{
    return std::tie(std::forward<Args>(args)...);
}