Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:55:13+00:00 2026-05-31T12:55:13+00:00

The following c++ code does not compile: int main() { double a = abs(5.1);

  • 0

The following c++ code does not compile:

int main() {
  double a = abs(5.1);
  return 0;
}

It complains that abs is not defined, of course. But the following does compile:

#include <iostream>

int main() {
  std::cout << abs(5.1) << std::endl;
  std::cout << abs(-5.1) << std::endl;
  return 0;
}

It outputs two 5’s (not 5.1’s). This is bad for lots of reasons. First, abs is such a natural and common function that I use it all the time, but the int part is almost never what I want returned. Second, it’s much too easy for me (or people using my code) to just write abs and not notice that it compiles but does the wrong thing, because I’m (they’re) really good at overlooking warnings. Third, I just plain don’t understand why iostream bothers defining an abs function anyway. Fourth, I really don’t understand why it goes into the global namespace.

Is there any way I can prevent this objectionable abs function from going into my global namespace?

If it matters, I’m using

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.6)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:55:14+00:00Added an answer on May 31, 2026 at 12:55 pm

    Most likely iostream includes stdlib.h to do some of its work. This is the C version of the header which declares abs for int only in the global namespace (in C you had to use fabs for double values).

    I’m not aware of any specific way to keep abs from being included that way but I do know that g++ 4.5 is much better at not having excess stuff brought in by basic includes like iostream and string.

    It may also be possible to get a warning that the double is being truncated to int (EDIT: yes, use -Wconversion to warn).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code does not compile: //int a = ... int? b = (int?)
The following code does not compile: public class GenericsTest { public static void main(String[]
I have the following bit of legacy C++ code that does not compile: #include
The following code does not compile with gcc 4.7 (20120114) , but compiles fine
Following code does compile in gcc 4.5 but it is not being compiled in
The following code compiles in MSVC++, but does not compile in GCC 4.5.1: #include
The following code does not compile. int a = 1, b = 2, c
Why does the following code compile: int main() { int j = 1; int
I am sure that the following code should not compile. But, in g++, it
The following code does not compile, why is that? And how can I fix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.