簡體   English   中英

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** 但是, const TestStruct *TestStruct**都沒有問題。 為什么 ?

您的問題不是來自 bind 本身,而是來自將T**T const**是非法的簡單事實。

有關原因的解釋,請參見http://c-faq.com/ansi/constmismatch.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM