C++ 常量指针作为std::bind参数
下面的代码C++ 常量指针作为std::bind参数,c++,stdbind,C++,Stdbind,下面的代码 #include <functional> #include <iostream> using namespace std; struct TestStruct { int c; }; int f(int a, int b, const TestStruct **t) { return a + b + (*t)->c; } void main() { TestStruct *t; bind(&f, 1, 2, &t)
#include <functional>
#include <iostream>
using namespace std;
struct TestStruct {
int c;
};
int f(int a, int b, const TestStruct **t) { return a + b + (*t)->c; }
void main() {
TestStruct *t;
bind(&f, 1, 2, &t)();
}
问题似乎是
const TestStruct**
param的常量。但是,const TestStruct*
和TestStruct**
都没有问题。为什么?您的问题不是来自绑定本身,而是来自一个简单的事实,即将T**
强制转换为T const**
是非法的
有关原因的解释,请参阅。您的问题不是来自绑定本身,而是来自一个简单的事实,即将
T**
强制转换为T const**
是非法的
有关原因的解释,请参见。您是否打算编写
TestStruct**const
?@LightnessRacesinOrbit No。我已经简化了f
函数,它是一个外部C
函数,我想绑定到它,它的签名就是这样。您不能。顺便问一下:main
返回int
,而不是void
。您是否打算编写TestStruct**const
?@LightnessRacesinOrbit否。我已经简化了,f
函数是一个我想要绑定到的外部C
函数,它的签名是这样的。您不能。顺便说一下:main
返回int
,而不是void
。
error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
note: With the following template arguments:
note: '_Callable=int (__cdecl *&)(int,int,const TestStruct **)'
note: '_Types={int &, int &, TestStruct **&}'