Pages

Saturday, 1 October 2022

A problem caused by C++ function overloading...

 Do you know the output of below code?

#include <iostream>
using namespace std;

int move(const int& x)
{
        cout << "hello\n";
        return 2;
}

int main()
{
        int x = 1;
        cout << move(x);
}

Actually, the function "move" defined here was not called at all!
The one called is std::move() defined in STL library.

The reasson is move(x) matches std::move(int&) more than move(const int& x) defined here.
So, be careful!!!

No comments:

Post a Comment