0

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?

2
  • 1
    Some functions are adopted from the global namespace regarding their plain c implementations are available. Commented Oct 8, 2018 at 19:10
  • mostly, in a module file, i would declare using namespace XXX; and use without XXX::, but in headers, i would not declare using namespace XXX; so it wont affect includers without their will. Commented Oct 8, 2018 at 19:16

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.