我已經閱讀了許多關於variadic模板和std :: bind的文章,但我想我仍然不理解它們是如何一起工作的。我認爲當涉及到使用可變參數模板時,我的概念有點模糊,使用了什麼std :: bind以及它們如何結合在一起。關於std :: ref()和std :: bind()與variadic模板有點朦朧
在下面的代碼中,我的lambda對TestClass類型的對象使用了點運算符,但即使當我傳入類型爲std :: ref的對象時,它們仍然可以工作。這究竟是怎麼回事?隱式轉換如何發生?
#include <iostream>
using std::cout;
using std::endl;
#include <functional>
#include <utility>
using std::forward;
class TestClass {
public:
TestClass(const TestClass& other) {
this->integer = other.integer;
cout << "Copy constructed" << endl;
}
TestClass() : integer(0) {
cout << "Default constructed" << endl;
}
TestClass(TestClass&& other) {
cout << "Move constructed" << endl;
this->integer = other.integer;
}
int integer;
};
template <typename FunctionType, typename ...Args>
void my_function(FunctionType function, Args&&... args) {
cout << "in function" << endl;
auto bound_function = std::bind(function, args...);
bound_function();
}
int main() {
auto my_lambda = [](const auto& one, const auto& two) {
cout << one.integer << two.integer << endl;
};
TestClass test1;
TestClass test2;
my_function(my_lambda, std::ref(test1), std::ref(test2));
return 0;
}
更具體地說,我通過在與兩個TestClass
對象test1
和test2
一個的reference_wrapper的兩個實例,但是當我將它們傳遞到拉姆達的.
操作符神奇。我希望你有使用::get()
功能中的reference_wrapper,使這項工作,但在調用.integer
數據成員的工作..
*「隱式轉換是如何發生的?」 - 隱式轉換什麼類型? – soon
我會更新我的問題,以便更清楚。對不起,關於 – Curious
@soon那裏!這是否使我的問題更清楚? – Curious