jeudi 29 juin 2023

Unexpected output when invoking overloaded functions with different rvalue reference types

I'm encountering an unexpected behavior in my code when calling overloaded functions with different rvalue reference types. The code snippet below demonstrates the issue:

#include <iostream>

void foo(int&& x)
{
    std::cout << "int Rvalue: " << x << std::endl;
}

void foo(float&& x)
{
    std::cout << "float Rvalue: " << x << std::endl;
}

int main()
{
    int int_x = 1;
    float float_x = 5.14f;

    foo(int_x);
    foo(float_x);
}

Output

float Rvalue: 1
int Rvalue: 5

Instead of getting the anticipated output, which should be "int Rvalue: 1" and "float Rvalue: 5", I observe the reverse order. I find this behavior perplexing and would appreciate insights into why this is happening.

I have made sure to understand the concept of rvalue references and how overloading works based on them. I have also tried to search for similar issues, but haven't found any that directly address this specific situation.

Any guidance or explanations would be greatly appreciated. Thank you in advance for your assistance!

Aucun commentaire:

Enregistrer un commentaire