Thread: Why is std:: not needed in front of C functions?

  1. #1
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    406

    Why is std:: not needed in front of C functions?

    Is it standards-conforming that you don't need to put std:: in front of functions from the C library in gcc? E.g.,
    Code:
    // g++ -std=c++14 -Wall -Wextra -pedantic -o wtf wtf.cpp
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    int main() {
        srand(time(0));
        std::cout << rand() << '\n';
    }
    It seems that clang doesn't complain, either.
    Good artists copy, great artists steal. - Picasso

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,292
    Yes it is standards conforming. See this section of the C++17 standard (from the N4594 draft):

    D.3 C standard library headers [depr.c.headers]
    1 For compatibility with the C standard library and the C Unicode TR, the C ++ standard library provides the
    26 C headers, as shown in Table 172.
    2 Except for the functions declared in 26.10, every C header, each of which has a name of the form name.h,
    behaves as if each name placed in the standard library namespace by the corresponding cname header is
    placed within the global namespace scope. It is unspecified whether these names are first declared or defined
    within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope
    by explicit using-declarations (7.3.3).

    3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace
    std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly
    provides the same declarations and definitions within the global namespace, much as in the C Standard. It
    may also provide these names within the namespace std. — end example ]

  3. #3
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    406
    I was just coming back to post that same quote (but from section D.5 in the C++14 n4296 draft).
    That just seems idiotic, though.
    I wonder if it's just a concession to ease compiler implementation.
    At any rate, it does mean that if you use, e.g., cstdlib, then you need to say std::rand for portability.
    Last edited by john.c; 07-08-2018 at 05:09 PM.
    Good artists copy, great artists steal. - Picasso

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    531
    Quote Originally Posted by john.c View Post
    I wonder if it's just a concession to ease compiler implementation.
    I imagine it's more to make it easier for people to migrate from C to C++. Most valid C programs can compile as C++ without any changes.

  5. #5
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    406
    Quote Originally Posted by christop View Post
    I imagine it's more to make it easier for people to migrate from C to C++. Most valid C programs can compile as C++ without any changes.
    That can't be the reason since including a C header as <stdlib.h> is guaranteed to dump the identifiers into the global namespace (but can additionally put them in std). Including it as <cstdlib> is guaranteed to put the identifiers in std (but can additionally dump them globally).
    Good artists copy, great artists steal. - Picasso

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    531
    Quote Originally Posted by john.c View Post
    That can't be the reason since including a C header as <stdlib.h> is guaranteed to dump the identifiers into the global namespace (but can additionally put them in std). Including it as <cstdlib> is guaranteed to put the identifiers in std (but can additionally dump them globally).
    Oh, I see what you mean. I must have skimmed the part about including cstdlib vs stdlib.h. In that case, yes, it might be to ease compiler implementation, or it could be written that way in the standard because existing compilers do it that way. It's basically a warning to the application writer that those function names might be defined at the global namespace, so the application writer cannot use those function names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-14-2013, 04:31 AM
  2. Functions,factorials asap help needed! :(
    By Solarwin in forum C Programming
    Replies: 13
    Last Post: 12-04-2012, 01:00 PM
  3. Replies: 9
    Last Post: 10-08-2012, 06:48 AM
  4. Add to Front, Remove from Front, Traverse
    By Brandon Smith in forum C Programming
    Replies: 9
    Last Post: 04-10-2012, 03:26 PM
  5. Explanation of functions needed.
    By JOZZY& Wakko in forum C Programming
    Replies: 4
    Last Post: 12-16-2009, 12:09 PM

Tags for this Thread