Angelus Mortis
Angelus Mortis

Reputation: 1554

Why is explicit call to operator<< ambiguous?

Here is the simple code :

int main()
{
    int x=0;
    std::cout<<x; 
    operator<<(std::cout,x); //ambiguous

    return 0;
}

Why is the operator<<(std::cout,x) call ambiguous but not std::cout<<x;? Thanks

Upvotes: 2

Views: 223

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

The problem here is that for outputting integers, operator<< is an std::ostream member function.

So to explicitly call the operator function you should do e.g.

std::cout.operator<<(x);

The stand-alone operator<< function is for characters and strings.

Upvotes: 6

Related Questions