0

I had a quick question. Is there a way to force the standard namespace to have the std:: scope qualifier? Something like abs and what not; all std functions have to have their scope defined.

I'm not using namespace std anywhere nor am I using std::abs or something like that. It might be important to say I have Windows.h included, which I feel may be where some of these unscoped functions are originating from.

2
  • What version of C++ are you using? It's possible that the operating system and/or compiler have their own versions of these functions (like abs etc) which is why std is not required, but std::abs does exist so you can still qualify it Commented Feb 21, 2019 at 4:14
  • I'm using C++ 17 and compiling with Visual Studio 2019 (which is the same compiler as 2017, I believe, but it has C++ 20 support) Commented Feb 21, 2019 at 4:28

3 Answers 3

2

The standard does not forbid other namespaces to have functions that are named abs just because there are functions named abs in std namespace. That's true of all other functions defined in the std namespace.

If you want to be certain that you use std::abs. and not any other version of abs, then by all means, use std::abs. Then, it's immaterial whether there are other versions of abs defined in the windows.h header file.

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

Comments

0

Calling abs instead of std::abs you are simply using abs from C

Comments

0

The rule in C++ is that #include <cmath> puts the declaration of abs in the namespace std and is allowed to also put the declaration of abs in the global namespace.

Another rule in C++ is that #include <math.h> puts the declaration of abs int he global namespace and is allowed to also put the declaration of abs in the namespace std.

So, no, you can't assume that #include <cmath> won't put abs into the global namespace.

The reason for this rule instead of the "obvious" one that #include <cmath> puts names only in std is that the latter rule can be nasty to implement if the C++ implementors don't also have control over the C headers. In that case, the C++ headers would have to have duplicate declarations for all of the C functions, and any change to the C headers would have to be carried through into the C++ headers, which produces a logistical nightmare.

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.