How to make an ambiguous call different from C ++?

void outputString(const string &ss) {
    cout << "outputString(const string& ) " + ss << endl;
}

void outputString(const string ss) {
    cout << "outputString(const string ) " + ss << endl;
}

int main(void) {
    //! outputString("ambigiousmethod"); 
    const string constStr = "ambigiousmethod2";
    //! outputString(constStr);
} ///:~

How to make a great call?

EDIT : This piece of code can be compiled using g ++ and MSVC.

thank.

+3
source share
4 answers

C ++ does not allow you to overload functions, where the only difference in the signature of the function is that the object accepts the object and the other refers to the object. So something like:

void foo(int);

and

void foo(int&);

is not allowed.

You need to change the number and / or type of parameter.

In your case, the function accepting reference, you can force it to accept pointer, if you want to allow the function to change its argument.

+10
source

. , .

,

void outputString(const string &ss, int notneeded) {
    cout << "outputString(const string& ) " + ss << endl;
}

void outputString(const string ss) {
    cout << "outputString(const string ) " + ss << endl;
}

, :

outputString("ambigiousmethod", 0);

.

( , ), ++ , ( ) .

: bzabhi, , . , , , .

+1

,

void outputString(const string &ss).

( const ).

2 ?

0

I recommend using techinque to provide each function with a unique name, i.e. Do not use syntax overload. I have been using it for many years, and I only found advantages in it.

0
source

Source: https://habr.com/ru/post/1726525/


All Articles