在可变参数模板中用lambda调用std :: make_tuple-这应该在C ++ 11中工作吗? [英] Calling std::make_tuple with lambdas in a variadic template - is this supposed to work in C++11?

查看:105
本文介绍了在可变参数模板中用lambda调用std :: make_tuple-这应该在C ++ 11中工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in C++11 a lambda function is an object, and it should be possible to call make_tuple with it, right?

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

此代码对我有用.

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

Now what happens if we add a variadic template:

#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中进行编译

This one fails to compile in 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

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

I wonder, is this code correct by the Standard ?

推荐答案

这是gcc中的已知错误: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933 (最初的示例是一个覆盖参数包捕获的不同模式,但是隐藏在其中的问题和合并的bug是gcc根本没有实现lambda和variadics的交集.

This is a known bug in gcc: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933 (the initial example is a different pattern covering capture of parameter packs, but the issue underlying that and the merged bugs is that gcc simply hasn't implemented the intersection of lambdas and variadics).

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

As far as the standard is concerned it's perfectly acceptable code.

这篇关于在可变参数模板中用lambda调用std :: make_tuple-这应该在C ++ 11中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆