简体   繁体   English

const 指针作为 std::bind 参数

[英]const pointer as std::bind parameter

The following code以下代码

#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)();
}

reports this error报告这个错误

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 **&}'

It seems that the problem is the constness of the const TestStruct** param.似乎问题在于const TestStruct**参数的const TestStruct** However, there's no problem neither with const TestStruct * nor with TestStruct** .但是, const TestStruct *TestStruct**都没有问题。 Why ?为什么 ?

Your problem does not come from bind itself, but from the simple fact that casting T** to T const** is illegal.您的问题不是来自 bind 本身,而是来自将T**T const**是非法的简单事实。

see http://c-faq.com/ansi/constmismatch.html for an explanation as to why.有关原因的解释,请参见http://c-faq.com/ansi/constmismatch.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM