Guy
Guy

Reputation: 115

Correct programming with namespaces

I wonder what is the right way to use namespace while calling functions or using classes from libraries. Should I always call a function in the format namespace::func()?

I'm a little bit confused because when I'm trying to use the function cout from <iostream> I always need to add the library namespace - std::cout.

But when I'm trying to call a function from <ctime> library which belongs to std namespace, I don't need to add the namespace before the function name.

Why is that?

Upvotes: 0

Views: 63

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

The <ctime> header is a backward-compatibility header file against the old C roots of C++. Such compatibility headers can (and usually do) put their function both in the std namespace and in the global namespace.

Thus a function like std::time can be reached both as std::time and time.

Upvotes: 7

Related Questions