2017-10-29 147 views
0

编辑:我明白我可以创建一个命名的函数来解决这个问题。我只想从你的大师那里学习一些神秘的C++模板语法。如何写std :: bind与构造函数


鉴于此设置(矢量保证是初始为空)

typedef vector<list<int>> V; 
V v; 

我想创建这样一个部分应用程序:

不幸的是,在我们的工作环境,我们不能使用lambda(使用gcc 4.4)。任何人都可以帮助我使用std::bind写一个等效版本吗?

到目前为止,我想出了一些尝试(使用Visual Studio):

1.

auto f = bind<void (V::*)(const V::value_type&)>(&V::push_back, &v, list<int>(1, placeholders::_1)); 

错误

'<function-style-cast>': cannot convert from 'initializer list' to 'std::list<int,std::allocator<_Ty>>' 

2.

auto f = bind<void (V::*)(const V::value_type&)>(&V::push_back, &v, list<int>::list(1, placeholders::_1)); 

与错误

'std::list<int,std::allocator<_Ty>>::list': none of the 10 overloads could convert all the argument types 

3.

auto f = bind<void (V::*)(const V::value_type&)>(&V::push_back, &v, list<int> { placeholders::_1 }); 

错误

'initializing': cannot convert from 'initializer list' to 'std::list<int,std::allocator<_Ty>>' 

4.

auto f = bind<void (V::*)(const V::value_type&)>(&V::push_back, &v, list<int> { (const int&) placeholders::_1 }); 

这个编译,而是由f(10)创建的载体将具有与第一清单元素等于0,而不是10.它看起来像列表是默认创建的列表。

+0

为什么它需要'std :: bind'?为什么不自己写一个本地课? –

+0

有其他方式可以做部分申请吗? –

+0

@crend lambdas只是匿名类对象。翻译是机械... – Yakk

回答

0

当std :: bind是boost :: bind或boost :: lambda :: bind时,有boost :: lambda :: constructor来绑定构造函数。

std :: bind是有限制的,它不能做一些boost :: bind或boost :: lambda :: bind的事情。从标准的角度来看,它不应该是一个大问题,因为在C++ 11中,你应该拥有C++ 11 lambda表达式。

相关问题