1

This is solely for example I happen to notice with!

I'm using cout with operator<< and why won't this program compile? Why aren't they being considered the way function overloadings are?

#include <iostream> // imports the declaration of std::cout

using namespace std; // makes std::cout accessible as "cout"

int cout() // declares our own "cout" function
{
    return 5;
}

int main()
{
    cout << "Hello, world!"; // Compile error!

    return 0;
}
Blake
  • 842
  • 6
  • 16
  • 1
    That's what `using namespace std;` means: treat **all** identifiers that are defined in `std` as if they were defined in the global namespace. So when you add **another** definition of `cout` to the global namespace you get a conflict. Short answer: **don't write `using namespace std;`.** – Pete Becker Mar 31 '19 at 20:29
  • 8
    `Why aren't they considered as function overloading when they're used differently?` because `std::cout` isn't a function. – tkausl Mar 31 '19 at 20:29
  • I apologize for closing this as duplicate. While you're asking a slightly different question, I believe you will find the answer in that post. – paddy Mar 31 '19 at 20:30
  • @paddy -- I reopened the question, since you say you closed it in error. – Pete Becker Mar 31 '19 at 20:34
  • @tkausl I got it. Compiler was unable to distinguish between `cout()`'s identifier and `std::cout`. – Blake Mar 31 '19 at 20:40
  • @tkausl -- but even `std::cout` was a function, there is no function call, so there would be no overloading. – Pete Becker Mar 31 '19 at 20:41
  • 2
    Note that it is perfectly possible to use an object name with parentheses and to use a function name without parentheses, so a compiler can't distinguish between a function and an object with the same name based on whether they are used with parentheses. – eesiraed Mar 31 '19 at 20:55

1 Answers1

3

At the point of the attempted stream insertion, there are two names cout in global scope: one from the standard library, std::cout, pulled into global scope by that infernal using declaration, and one defined as a function int cout(). In the expression

cout << "Hello, world!\n";

the use of cout is ambiguous. There is no function overloading for two reasons: first, std::cout is not a function, so would not take part in overloading. But more fundamentally, the use of cout in that expression is not a function call, so, again, there is no overloading. The name cout from the function definition is treated as a pointer to function, and the name cout from namespace std is the name of an object. There are two possible interpretations of that name, so its use in that << expression is ambiguous.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • That's a good explanation. But I don't know why people down voting my question. If possible, I'd like to keep this alive for future readers who are confused like me. – Blake Mar 31 '19 at 20:45
  • 3
    @MichaelD.Blake My best guess is that it's the unfortunate habit of many SO users to downvote questions if the problem is partially due to bad practice, i.e. "if you didn't do *this*, the question wouldn't have come up", even though the actual subject matter of the question is not really related to the bad practice. And while `using namespace std;` is not a particularly good idea, the real bad practice as in "why would you even do that?" is naming a function `cout`. But this is just a guess, I'm not a mind reader. – Sebastian Redl Apr 01 '19 at 06:56
  • @SebastianRedl I know it's a bad practice and all. But I happen to notice it with `cout` and came here right away. Maybe I should put this up with another example without even any namespace? – Blake Apr 01 '19 at 07:15
  • @MichaelD.Blake No, your question is fine IMO. I was just trying to explain why you're getting downvotes. Sometimes you just have to live with it. Haters gonna hate. – Sebastian Redl Apr 01 '19 at 08:40
  • @SebastianRedl I agree, but this is a community and if I don't make the majority feel happy, I won't be able to ask anything. – Blake Apr 01 '19 at 08:49
  • @MichaelD.Blake You're getting more upvotes than downvotes, so the majority is happy ;-) – Sebastian Redl Apr 01 '19 at 09:57