const 指针作为 std::bind 参数
const pointer as std::bind parameter
代码如下
#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)();
}
报告此错误
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 **&}'
似乎问题出在 const TestStruct**
参数的常量性上。但是,const TestStruct *
和 TestStruct**
都没有问题。为什么?
你的问题不是来自 bind 本身,而是来自将 T**
转换为 T const**
是非法的简单事实。
有关原因的解释,请参阅 http://c-faq.com/ansi/constmismatch.html。
代码如下
#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)();
}
报告此错误
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 **&}'
似乎问题出在 const TestStruct**
参数的常量性上。但是,const TestStruct *
和 TestStruct**
都没有问题。为什么?
你的问题不是来自 bind 本身,而是来自将 T**
转换为 T const**
是非法的简单事实。
有关原因的解释,请参阅 http://c-faq.com/ansi/constmismatch.html。