2

在 Visual Studio 2019 中,我编写了以下测试代码,但结果让我很困惑。

#include <iostream>
using namespace std;

template<class T, class Func>
int call(T x, Func f) { return f(x); }

int square(int x) { return x * x; }

int main() {

    int (*func0) (int) = square; // line 0, OK
    //int (func1)(int) = square; // line 1, wrong

    int (__cdecl *func1) (int)  = square; // line 2, OK
    //int (__cdecl func2)(int) = square; // line 3, wrong

    cout << ((int(__cdecl*)(int)) square)(5) << endl; // line 4, OK
    //cout << ((int(__cdecl)(int)) square)(5) << endl; // line 5, wrong

    cout << call<int, int (*)(int)>(5, square) << endl; // line 6, OK
    //cout << call<int, int ()(int)>(5, square) << endl; // line 7, wrong

    cout << call<int, int(__cdecl*)(int)>(5, square) << endl; // line 8, OK
    cout << call<int, int(__cdecl)(int)>(5, square) << endl; // line 9, OK

    return 0;
}

(我知道我可以在使用时省略类型call,但这是一个实验。)

我以为我能够理解从第 0 行到第 7 行的所有内容。我想到的square是一个函数指针,所以它应该有 typeint (*) (int)或者可能int(__cdecl*) (int),这两个要么相同,要么可以相互转换(我没有改变项目的调用约定,所以默认是__cdecl)。

然而,令我惊讶的是第 8 行和第 9 行都能正确编译和运行。为什么会这样?

通过将第 6、7 行与第 8、9 行进行比较,我认为问题来自添加__cdecl,但在Microsoft Docs中没有提到这样的内容。


然后我打印出类型:


    // ...
    cout << typeid(square).name() << endl; // output: int __cdecl(int)
    cout << typeid(*square).name() << endl; // output: int __cdecl(int)
    cout << typeid(&square).name() << endl; // output: int(__cdecl*)(int)

    cout << (typeid(square) == typeid(int(*) (int))) << endl; // output: false
    cout << (typeid(square) == typeid(int(__cdecl) (int))) << endl; // output: true
    cout << (typeid(square) == typeid(int(__cdecl*) (int))) << endl; // output: false

    cout << (typeid(square) == typeid(*square)) << endl; // output: true
    // ...

看来square确实有类型int (__cdecl) (int)。另外,我不明白为什么square并且*square属于同一类型...

有人可以向我解释这些现象吗?

4

2 回答 2

2

square并且*square是相同的类型,因为函数像数组一样衰减到指针,除了(就像数组一样)在某些上下文中。特别是,衰减在typeid和下被抑制&,但在 下没有*,所以typeid(square)给你 , 的类型squareint (__cdecl)(int)typeid(*square)手段typeid(*&square)手段typeid(square)给出同样的东西。这导致了一个奇怪的事实,即您可以编写任意数量*的 s 并且它们都不会做任何事情:*************squaresquare.

现在,对于剩下的问题,你写的类型“函数int返回int”是错误的。int ()(int)意思是“函数不带参数返回函数int返回int”。你想要int(int)。然后这个工作:

cout << call<int, int(int)>(5, square) << "\n"; // line 7, fixed (FYI: endl is not normally necessary)

因为现在call有参数列表(int x, int f(int)),并且函数类型的参数声明会自动调整为具有指向函数类型的指针,从而在call<int, int(int)>功能上与call<int, int (*)(int)>. (这不适用于变量声明或强制转换,因此第 1、3、5 行仍然不正确。)额外的括号导致类型被误解。第 9 行有效,因为将括号放在__cdecl括号内使它们不会被误解(它们不是函数声明符,而是分组符号)。

cout << call<int, int (__cdecl)(int)>(5, square) << "\n"; // line 9, OK

再次call调整 's 参数的类型。int (__cdecl f)(int)变为int (__cdecl *f)(int),这使得第 9 行在功能上与第 8 行相同。

于 2020-06-17T22:10:37.453 回答
0

该行的错误:

int (func1)(int) = square; // line 1, wrong

是你错过了一个'*',它需要是:

int (*func1)(int) = square; // line 1, wrong

//int (__cdecl func2)(int) = square; // line 3, wrong

cout << ((int(__cdecl)(int)) square)(5) << endl; // line 5, wrong

需要是

cout << ((int(__cdecl*)(int)) square)(5) << endl; // line 5, wrong
于 2020-06-17T22:01:43.790 回答