std::function 内部模板 class 给出对象创建错误
std::function inside template class giving object creation error
下面给出的代码出现以下错误。尝试在模板 class 中包含 3 std::function,然后使用来自不同 classes 的不同函数初始化它们。
为什么会出现这个错误?下面的代码有什么问题?
template <typename T>
class CFuntion {
public:
std::function<void()> callback_1;
std::function<void()> callback_2;
std::function<void()> callback_3;
template <typename A, typename B, typename C>
CFuntion(A cb1, B cb2, C cb3)
: callback_1(cb1), callback_2(cb2), callback_3(cb3)
{ }
};
class Impl1 {
public:
void do_something1() {
cout << "Inside Impl1 do_something1" << endl;
}
void do_something2() {
cout << "Inside Impl1 do_something2" << endl;
}
};
class Impl2 {
public:
void do_something1() {
cout << "Inside Impl2 do_something1" << endl;
}
void do_something2() {
cout << "Inside Impl2 do_something2" << endl;
}
};
class Impl3 {
public:
void do_something1() {
cout << "Inside Impl3 do_something1" << endl;
}
void do_something2() {
cout << "Inside Impl3 do_something2" << endl;
}
};
int main()
{
unique_ptr<CFuntion<void>> ptr1 = make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1, &Impl2::do_something1,
&Impl3::do_something2));
ptr1->callback_1();
ptr1->callback_2();
ptr1->callback_3();
system("pause");
return 0;
}
看这部分:
unique_ptr<CFuntion<void>> ptr1 = make_unique<CFuntion<void>(std::bind(&Impl1::do_something1, &Impl2::do_something1, &Impl3::do_something2));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
在这里,您只将一个对象传递给 CFunction<void>
的构造函数。您需要的可能是:
// You need to pass temporary objects to 'std::bind()' to bind non-static member functions
Impl1 obj1;
Impl2 obj2;
Impl3 obj3;
std::unique_ptr<CFuntion<void>> ptr1 = std::make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1, obj1), std::bind(&Impl2::do_something1, obj2), std::bind(&Impl3::do_something2, obj3));
或者,通过查看您的 Impl1
、Impl2
和 Impl3
类,您似乎可以将它们的成员方法设为 static
(如 none 这三个 类 似乎需要在它们的对象之间进行任何类型的区分,或者有任何实例变量到 access/modify。):
class Impl1 {
public:
static void do_something1() {
cout << "Inside Impl1 do_something1" << endl;
}
static void do_something2() {
cout << "Inside Impl1 do_something2" << endl;
}
};
class Impl2 {
public:
static void do_something1() {
cout << "Inside Impl2 do_something1" << endl;
}
static void do_something2() {
cout << "Inside Impl2 do_something2" << endl;
}
};
class Impl3 {
public:
static void do_something1() {
cout << "Inside Impl3 do_something1" << endl;
}
static void do_something2() {
cout << "Inside Impl3 do_something2" << endl;
}
};
这样,你只需要这样做,这似乎是你想要做的:
std::unique_ptr<CFuntion<void>> ptr1 = std::make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1), std::bind(&Impl2::do_something1), std::bind(&Impl3::do_something2));
下面给出的代码出现以下错误。尝试在模板 class 中包含 3 std::function,然后使用来自不同 classes 的不同函数初始化它们。
为什么会出现这个错误?下面的代码有什么问题?
template <typename T>
class CFuntion {
public:
std::function<void()> callback_1;
std::function<void()> callback_2;
std::function<void()> callback_3;
template <typename A, typename B, typename C>
CFuntion(A cb1, B cb2, C cb3)
: callback_1(cb1), callback_2(cb2), callback_3(cb3)
{ }
};
class Impl1 {
public:
void do_something1() {
cout << "Inside Impl1 do_something1" << endl;
}
void do_something2() {
cout << "Inside Impl1 do_something2" << endl;
}
};
class Impl2 {
public:
void do_something1() {
cout << "Inside Impl2 do_something1" << endl;
}
void do_something2() {
cout << "Inside Impl2 do_something2" << endl;
}
};
class Impl3 {
public:
void do_something1() {
cout << "Inside Impl3 do_something1" << endl;
}
void do_something2() {
cout << "Inside Impl3 do_something2" << endl;
}
};
int main()
{
unique_ptr<CFuntion<void>> ptr1 = make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1, &Impl2::do_something1,
&Impl3::do_something2));
ptr1->callback_1();
ptr1->callback_2();
ptr1->callback_3();
system("pause");
return 0;
}
看这部分:
unique_ptr<CFuntion<void>> ptr1 = make_unique<CFuntion<void>(std::bind(&Impl1::do_something1, &Impl2::do_something1, &Impl3::do_something2));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
在这里,您只将一个对象传递给 CFunction<void>
的构造函数。您需要的可能是:
// You need to pass temporary objects to 'std::bind()' to bind non-static member functions
Impl1 obj1;
Impl2 obj2;
Impl3 obj3;
std::unique_ptr<CFuntion<void>> ptr1 = std::make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1, obj1), std::bind(&Impl2::do_something1, obj2), std::bind(&Impl3::do_something2, obj3));
或者,通过查看您的 Impl1
、Impl2
和 Impl3
类,您似乎可以将它们的成员方法设为 static
(如 none 这三个 类 似乎需要在它们的对象之间进行任何类型的区分,或者有任何实例变量到 access/modify。):
class Impl1 {
public:
static void do_something1() {
cout << "Inside Impl1 do_something1" << endl;
}
static void do_something2() {
cout << "Inside Impl1 do_something2" << endl;
}
};
class Impl2 {
public:
static void do_something1() {
cout << "Inside Impl2 do_something1" << endl;
}
static void do_something2() {
cout << "Inside Impl2 do_something2" << endl;
}
};
class Impl3 {
public:
static void do_something1() {
cout << "Inside Impl3 do_something1" << endl;
}
static void do_something2() {
cout << "Inside Impl3 do_something2" << endl;
}
};
这样,你只需要这样做,这似乎是你想要做的:
std::unique_ptr<CFuntion<void>> ptr1 = std::make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1), std::bind(&Impl2::do_something1), std::bind(&Impl3::do_something2));