【发布时间】:2022-01-07 00:17:34
【问题描述】:
int* b = new int(40);
int c = *(int *)b;
上述演员工作正常。
但类似的转换不适用于函数指针
void abc(int a){
cout<<a<<endl;
}
std::function<void(int)> callback = *(std::function<void(int)>*)abc; // this cast is not working
上面的代码有什么问题?
【问题讨论】:
-
是什么让你认为
void(*)(int)是std::function<int>*? -
@Raildex 对于刚接触 C++ 或来自其他语言的人来说,假设函数是函数是函数并不是不合理的。事实上,C++ 有 3(4?)种半兼容的方式来表示一段可调用代码的句柄,这基本上是历史的偶然
-
你根本不需要演员表:godbolt.org/z/zes3nW1WK
-
你明白
int c = *(int *)b;和int c = (int)*b;的区别吗?
标签: c++ pointers type-conversion