The*_* do 3 c++ std visual-c++ c++11 visual-studio-2015
此代码在VS2015更新1中给出了错误:
错误C2893:无法专门化函数模板'unknown-type std :: invoke(_Callable &&,_ Types && ...)'
#include <iostream>
#include <functional>
using std::cout;
class A
{
public:
virtual void init()
{
cout << "A";
};
};
class B
{
public:
virtual void init()
{
cout << "B";
};
};
class C : private A, private B
{
std::function<void()> a_init = &A::init;
std::function<void()> b_init = &B::init;
public:
void call()
{
a_init();
b_init();
}
};
int main()
{
C c;
c.call();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何想法,如果VS编译器是错误或我的代码?
编辑
#include "stdafx.h"
#include <functional>
class A
{
public:
virtual void inita()
{
cout << "A";
};
};
class B
{
public:
virtual void initb()
{
cout << "B";
};
};
class C : private virtual A, private virtual B
{
/*std::function<void()> a_init = &A::init;
std::function<void()> b_init = &B::init;*/
public:
void call()
{
inita();
}
};
Run Code Online (Sandbox Code Playgroud)
您正在尝试将非静态成员函数分配给不std::function带参数的函数.这不起作用,因为非静态成员函数具有隐式this参数.
如何解决这个问题取决于你想做什么.如果要在调用时提供的任意对象上调用存储函数,则需要更改std::function签名:
std::function<void(A*)> a_init = &A::init;
void call()
{
a_init(this); // or some other object of type A on which you want to invoke it
}
Run Code Online (Sandbox Code Playgroud)
另一方面,如果要在不带参数的情况下调用它,则必须将类型的对象绑定A到std::function初始化时:
std::function<void()> a_init = std::bind(&A::init, this);
void call()
{
a_init()
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
995 次 |
| 最近记录: |