gpt4 book ai didi

c++ - 在可变参数模板中使用 lambda 调用 std::make_tuple - 这应该在 C++11 中工作吗?

转载 作者:太空狗 更新时间:2023-10-29 19:53:30 37 4
gpt4 key购买 nike

在 C++11 中,lambda 函数是一个对象,应该可以用它调用 make_tuple,对吗?

void foobar() {
auto t = std::make_tuple([](){ std::make_shared<int>(); });
}

这段代码对我有用。

现在如果我们添加可变参数模板会发生什么:

#include <tuple>
#include <memory>

template <class... T>
void foobar() {
auto t = std::make_tuple([](){ std::make_shared<T>(); }...);
}

int main(int, char**)
{
foobar<int, float, double>();
return 0;
}

这个无法在 GCC 4.7.2 中编译

main.cpp: In lambda function:
main.cpp:6:54: error: parameter packs not expanded with '...':
main.cpp:6:54: note: 'T'
main.cpp: In function 'void foobar()':
main.cpp:6:57: error: expansion pattern '#'lambda_expr' not supported by dump_expr#<expression error>' contains no argument packs
main.cpp: In instantiation of 'void foobar() [with T = {int, float, double}]':
main.cpp:11:29: required from here

我想知道,这段代码是否符合标准?

最佳答案

这是 gcc 中的一个已知错误:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933 (最初的例子是一个不同的模式,涵盖了参数包的捕获,但潜在的问题和合并的错误是 gcc 根本没有实现 lambdas 和 variadics 的交集)。

就标准而言,它是完全可以接受的代码。

关于c++ - 在可变参数模板中使用 lambda 调用 std::make_tuple - 这应该在 C++11 中工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13469851/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com