Why is abs not in std?

Your question is slightly wrong. abs is in std. Let me assume that you're asking "Why is abs also in the global namespace?".

The C++ standard allows the C standard functions to be declared in the global namespace in addition to being declared in the std namespace.

Why is this happening?

Because those functions are from the C standard library, and because standard allows it to happen, and because of the way your standard library was implemented.

How can I use these functions without cluttering my namespace?

Those names are reserved to the language implementation in the global namespace. There's no way to avoid it.

Don't treat the global namespace as "your namespace". The global namespace is cluttered, and it always will be. You need to work around that.

If not possible, are there c++ alternatives?

The C++ alternative is to declare your abs in your namespace:

namespace my_very_own_namespace {
    int abs;
}

You should declare everything1 in your own namespace, except:

  • That namespace itself
  • main
  • Template specialisations of names in other namespaces where that is allowed
  • Cross-language API's i.e. anything extern "C"

The hardest part is figuring out a unique name for your own namespace, since it mustn't be one that is already used by the C standard library, nor preferably any name used by other libraries in the global namespace.

1 Following this rule of thumb is hardly necessary when writing tiny exercise programs etc. But it becomes essential when writing large programs and relying on third party libraries, not to mention when writing those libraries for others to use.


Standard quotes:

[extern.names]

Each name from the C standard library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.

Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both extern "C" and extern "C++" linkage,171 or as a name of namespace scope in the global namespace.

[headers]

Except as noted in [library] through [thread] and [depr], the contents of each header cname is the same as that of the corresponding header name.h as specified in the C standard library. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope of the namespace std. It is unspecified whether these names (including any overloads added in [support] through [thread] and [depr]) are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations ([namespace.udecl]).

Answer from eerorika on Stack Overflow
Top answer
1 of 1
7

Why is abs not in std?

Your question is slightly wrong. abs is in std. Let me assume that you're asking "Why is abs also in the global namespace?".

The C++ standard allows the C standard functions to be declared in the global namespace in addition to being declared in the std namespace.

Why is this happening?

Because those functions are from the C standard library, and because standard allows it to happen, and because of the way your standard library was implemented.

How can I use these functions without cluttering my namespace?

Those names are reserved to the language implementation in the global namespace. There's no way to avoid it.

Don't treat the global namespace as "your namespace". The global namespace is cluttered, and it always will be. You need to work around that.

If not possible, are there c++ alternatives?

The C++ alternative is to declare your abs in your namespace:

namespace my_very_own_namespace {
    int abs;
}

You should declare everything1 in your own namespace, except:

  • That namespace itself
  • main
  • Template specialisations of names in other namespaces where that is allowed
  • Cross-language API's i.e. anything extern "C"

The hardest part is figuring out a unique name for your own namespace, since it mustn't be one that is already used by the C standard library, nor preferably any name used by other libraries in the global namespace.

1 Following this rule of thumb is hardly necessary when writing tiny exercise programs etc. But it becomes essential when writing large programs and relying on third party libraries, not to mention when writing those libraries for others to use.


Standard quotes:

[extern.names]

Each name from the C standard library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.

Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both extern "C" and extern "C++" linkage,171 or as a name of namespace scope in the global namespace.

[headers]

Except as noted in [library] through [thread] and [depr], the contents of each header cname is the same as that of the corresponding header name.h as specified in the C standard library. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope of the namespace std. It is unspecified whether these names (including any overloads added in [support] through [thread] and [depr]) are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations ([namespace.udecl]).

🌐
GitHub
github.com › PX4 › Firmware › issues › 2158
C++ std::abs is not available when compiling C++ modules · Issue #2158 · PX4/PX4-Autopilot
May 11, 2015 - When using std::abs in code within a module, a compilation error: /Firmware/src/modules/test_module/test_module_main.cpp: In function 'int test_module_main(int, char**)': /Firmware/src/modules/test_module/test_module_main.cpp:8:18: error: 'abs' is not a member of 'std' printf("%d", std::abs(0.1)); A minimal test module showing the issue is in the following commit: molysgaard@fbce204 ·
Published   May 11, 2015
Author   molysgaard
Discussions

error: no member named 'abs' in namespace 'std'
Note that libc++ unexpectedly defines std::abs(float) in cmath and std::abs(int) in cstdlib. See http://en.cppreference.com/w/cpp/numeric/math/abs ... codebase/seqlib/src/libs/opal.cc:1304:45: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'? More on github.com
🌐 github.com
4
February 22, 2017
and namespace std | The FreeBSD Forums
The following snippet does not compile with g++ v4.8.2: #include int main() { double m = std::fmax(2., 8.); m = std::abs(2.); } The command g++48 -std=c++11 asd.cpp gives this error: asd.cpp: In function 'int main()': asd.cpp:6:14: error: 'fmax' is not a member of 'std' double m =... More on forums.freebsd.org
🌐 forums.freebsd.org
October 4, 2013
' ' is not a member of 'std' - C++ Forum
This is the main base of C++ Take ... std; ... The sum of the angles of any triangle are exactly 180 degrees. If they sum to less or more than they are not angles of a triangle. #However, as totalAngle is double doing an exact comparison with 180 is not guaranteed to be true even if the angles should add to 180 - due to the way non-integer numbers are represented in binary. The usual way to do such a comparison to check that the absolute value of num1 ... More on cplusplus.com
🌐 cplusplus.com
August 18, 2022
abs (built-in) function is not declared - C++ Forum
TUTORIALS · REFERENCE · ARTICLES · FORUM · Tutorials · Reference · Articles · Forum · Beginners More on cplusplus.com
🌐 cplusplus.com
February 28, 2015
🌐
GitHub
github.com › raceintospace › raceintospace › issues › 362
error: no member named 'abs' in namespace 'std' · Issue #362 · raceintospace/raceintospace
June 4, 2020 - /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_games_raceintospace/raceintospace/work/raceintospace-8ff7814d5e2b6e73a817ead6c76eeb8823d4e3d7/src/display/legacy_surface.cpp:91:13: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'? steep = std::abs((int)(y2 - y1)) > std::abs((int)(x2 - x1)); ^~~~~~~~ abs /usr/include/stdlib.h:129:6: note: 'abs' declared here int abs(int) __pure2; ^ /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_games_raceintospace/raceintospace/work/raceintospace-8ff7814d5e2b6e73a817ead6c76eeb88
Published   Jun 04, 2020
🌐
GitHub
github.com › isovic › racon › issues › 20
error: no member named 'abs' in namespace 'std' · Issue #20 · isovic/racon
February 22, 2017 - Note that libc++ unexpectedly defines std::abs(float) in cmath and std::abs(int) in cstdlib. See http://en.cppreference.com/w/cpp/numeric/math/abs ... codebase/seqlib/src/libs/opal.cc:1304:45: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'?
Published   Feb 22, 2017
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
and namespace std | The FreeBSD Forums
October 4, 2013 - The following snippet does not compile with g++ v4.8.2: #include int main() { double m = std::fmax(2., 8.); m = std::abs(2.); } The command g++48 -std=c++11 asd.cpp gives this error: asd.cpp: In function 'int main()': asd.cpp:6:14: error: 'fmax' is not a member of 'std' double m =...
🌐
SourceForge
sourceforge.net › home › browse › guitarix › bugs
guitarix / Bugs / #98 error: no member named 'abs' in namespace 'std'
May 28, 2020 - ../libgxw/gxw/GxTuner.cpp:283:21: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'? float pitch_add = std::abs(tuner->priv->reference_pitch - 440.00); ^~~~~~~~ abs /usr/include/c++/v1/math.h:789:1: note: 'abs' declared here · It seems like you forgot to include <cstdlib> or <cmath> , see https://en.cppreference.com/w/cpp/numeric/math/abs</cmath></cstdlib> brummer - 2020-05-28 ·
🌐
Cplusplus
cplusplus.com › forum › beginner › 284692
' ' is not a member of 'std' - C++ Forum
August 18, 2022 - Indeed answer is not a part of the std namspace, change std::answer to answer: double answer = = 0.5*(h*b); Although you probably meant area rather than answer. That code should be in a member function. Try to avoid variable names like h, b. It is more meaningful to name the height and base.
🌐
Cplusplus
cplusplus.com › forum › beginner › 158286
abs (built-in) function is not declared - C++ Forum
February 28, 2015 - #include <math.h> is provided. I get a compile error: error: ‘abs’ was not declared in this scope What is the problem?
Find elsewhere
🌐
JUCE
forum.juce.com › general juce discussion
[solved] Why does my Abs function crash but not std::abs? - General JUCE discussion - JUCE
June 2, 2018 - I hope it’s OK to ask generic C++ stuff here… I’m playing around with various optimizations and approximizations, just having fun and trying to squeeze the odd microsecond out here and there. So I wrote my own approach to a function that turns a value into its absolute (positive) value, basically to measure it against std::abs(). This is my implementation: namespace Math { template inline const TYPE Abs (const TYPE& Value) { return Value + (Value
🌐
CERN
root-forum.cern.ch › t › fabs-and-abs › 14556
Fabs and abs - ROOT - ROOT Forum
July 10, 2012 - Try simply this: [code]#if 1 /* 0 or 1 / #include <stdlib.h> // for “abs” #include <math.h> // for “fabs” #else / 0 or 1 / #include // for “std::abs” and “std::fabs” #endif / 0 or 1 */ · You should have got it by now that there’s no “default c++ behaviour” what concerns ...
🌐
GitHub
github.com › pavel-odintsov › fastnetmon › issues › 1008
error: 'setw' is not a member of 'std'; did you mean 'set' during absl compilation · Issue #1008 · pavel-odintsov/fastnetmon
June 30, 2024 - Error: [ 66%] Building CXX object absl/synchronization/CMakeFiles/absl_lifetime_test.dir/lifetime_test.cc.o [ 66%] Linking CXX executable ../../bin/absl_notification_test [ 66%] Built target absl_notification_test [ 66%] Building CXX obj...
Author   pavel-odintsov
🌐
MacPorts
trac.macports.org › ticket › 55785
#55785 (qpdf @7.1.0: error: no member named 'abs' in namespace 'std') – MacPorts
:info:build /bin/bash ./libtool --mode=link /usr/bin/clang++ -pipe -Os -stdlib=libc++ -arch x86_64 -Wold-style-cast -Wall -o libqpdf/build/libqpdf.la libqpdf/build/BitStream.lo libqpdf/build/BitWriter.lo libqpdf/build/Buffer.lo libqpdf/build/BufferInputSource.lo libqpdf/build/FileInputSource.lo libqpdf/build/InputSource.lo libqpdf/build/InsecureRandomDataProvider.lo libqpdf/build/MD5.lo libqpdf/build/OffsetInputSource.lo libqpdf/build/Pipeline.lo libqpdf/build/Pl_AES_PDF.lo libqpdf/build/Pl_ASCII85Decoder.lo libqpdf/build/Pl_ASCIIHexDecoder.lo libqpdf/build/Pl_Buffer.lo libqpdf/build/Pl_Concat
🌐
PlatformIO Community
community.platformio.org › development platforms
Conflict between std::abs() and macro for abs() in wiring.h (in library TeensyStep) - Development Platforms - PlatformIO Community
April 11, 2023 - Hi everyone, tl;dr When running the code below, the macro for abs defined in wiring.h interferes with the use of std::abs(). I stumbled across an issue which leaves me baffled I must admit. I was working for the last 2 years on a project with stepper motors that make use of std functions and ...
🌐
GitHub
github.com › google › or-tools › issues › 356
compilation failure due to switch from fabs to std::abs · Issue #356 · google/or-tools
March 28, 2017 - src/glop/basis_representation.cc: In member function ‘operations_research::glop::Fractional operations_research::glop::BasisFactorization::ComputeInverseOneNorm() const’: src/glop/basis_representation.cc:555:51: error: call of overloaded ‘abs(__gnu_cxx::__alloc_traits<std::allocator<double> >::value_type&)’ is ambiguous column_norm += std::abs(right_hand_side[row]); ^ In file included from src/base/logging.h:18:0, from src/glop/basis_representation.h:18, from src/glop/basis_representation.cc:15: /usr/include/stdlib.h:735:12: note: candidate: int abs(int) extern int abs (int __x) __THRO
Author   jmarca
🌐
GitHub
github.com › QupZilla › qupzilla › issues › 2169
error: no member named 'abs' in namespace 'std' · Issue #2169 · QupZilla/qupzilla
January 21, 2017 - It is now read-only. ... tools/wheelhelper.cpp:39:13: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'? if (std::abs(event->angleDelta().y()) > std::abs(event->angleDel... ^~~~~~~~ abs /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/stdlib.h:129:6: note...
Author   Martii
🌐
Blogger
lessonsincoding.blogspot.com › 2012 › 02 › ripped-abs-function-in-c.html
Lessons In Coding: The Ripped abs Function in C++
Why the compile error and what header file to include? Part of the confusion lies in the fact the abs function shows up in two different header files. Here’s what I learnt from the ISO C++ Standard documentation (N3337 2012-01-16). It’s known as C++11—the latest standard of C++, and I should have looked here first. <math.h> and <stdlib.h> are Standard C library headers. That's the C language, not C++, but since the C++ is a superset of C, the C++ Standard library headers <cmath> <cstdlib> are the same as in C except C++ extends them.