const pointer as std::bind parameter

538 views Asked by At

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. However, there's no problem neither with const TestStruct * nor with TestStruct**. Why ?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

Your problem does not come from bind itself, but from the simple fact that casting T** to T const** is illegal.

see http://c-faq.com/ansi/constmismatch.html for an explanation as to why.