将指针传递给具有引用参数的函数

Passing a pointer to a function with reference parameter

通常,这会被编译器捕获。但是使用转换为函数指针我们可以做到这一点。

我可能措辞不当,这里有一个例子可以更好地说明我的问题:

void printintp(int *ip)
{
    printf("pointer = %p\n", ip);
    printf("*pointer = %d\n", *ip);
}

void printint(int &i)
{
    printf("int = %d\n", i);
}

int main()
{
    int a = 555;
    //int &irp = &a;   // ERROR 1
    void (*f)(void *);

    //printint((int &) &a);   // ERROR 2
    f = (void (*)(void *))printintp;
    f(&a);
    f = (void (*)(void *))printint;
    //printint(&a);       // ERROR 3 (invalid conversion of `int *` to `int`)
    f(&a);              // GOOD (why?)
    return 0;
}

输出当然是人们所期望的:

pointer = 0x7ffd01995bf4
*pointer = 555
int = 555

所以我的问题细化为:为什么将指针 &a 传递给对象期望函数 printint OK?这种未定义的行为恰好适用于我的实现吗?如果不是,如果我们只传递它的地址,参数 int &i 如何绑定到对象 a 以及为什么它拒绝我标记为 ERROR 2 的内容?

PS:关于这个问题,我能找到的最好的是 this,它声称 printintpprintint “在功能上相同”,尽管在实践中, 函数调用不同 (printintp(&a)printint(a)).

PS2:请注意,我不是在问指针和引用之间的区别,这已经解释得很清楚了here

Is this undefined behavior that just happens to work on my implementation?

是的。大多数编译器在后台使用指针实现引用,但这不是标准规定的。

标准语法不允许将指针分配给 as-is 引用,反之亦然,因此编译器会在这些语句上出错。

将所有内容都转换为 void*,所有关于类型安全的赌注都落空了。使用 C-style type cast, you can get away with just about anything. You are basically telling the compiler that you know what you are doing, so it should just accept it and be quiet. That is why such type-casts can be dangerous when you don't know what you are really doing, and should be avoided unless absolutely necessary. If you must cast, prefer C++-style type casts instead (static_cast, reinterpret_cast,等等)。至少到那时,您不会放弃那么多的类型安全。