2017-10-29 148 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表達式。

相關問題