Mike Housky
Mike Housky

Reputation: 4069

C++ endl() without std:: prefix or using statement

Why does this use of endl() without a namespace qualifier compile? I've tried it on multiple compilers, searched through several pages of search here for "endl without std::", and I'm stumped as to why it's not even a warning.

#include <iostream>

int main(){
    endl(std::cout << "hello weirdness");
    return 0;
}

This is distilled from a bit of deliberately obfuscated code found at another site, and is not something I'd use in a real program. I just don't see why it doesn't complain that endl isn't defined.

Upvotes: 0

Views: 99

Answers (1)

Brian Bi
Brian Bi

Reputation: 119582

std::endl is a function template with the following signature:

template <class charT, class traits>
std::basic_ostream<charT, traits>& endl(std::basic_ostream<charT, traits>& os);

When the argument has type std::ostream, argument-dependent lookup occurs and the function name endl is searched for in the namespace std.

Upvotes: 7

Related Questions