3

I came to this conclusion when I was trying to figure out what was going on with the code below:

#include <iostream>

int main()
{
  std::cout << (div) << '\n';
  return 0;
}

div above could be substituted with printf, atoi, difftime, etc. Whether or not I was #includeing the appropriate headers (ctime, time.h, cstdlib, ..., ), I was getting no compiler errors and the program was printing 1. Program was not compiling when I was prefixing the function name with std::.

Clang's warnings explained what was going on:

warning: address of function 'div' will always evaluate to 'true' [-Wbool-conversion]

So my questions are:

  • Why do names of C library function have any meaning even when the appropriate headers are not #included?
  • Why are they defined as pointers to functions?
  • Why do they reside outside namespace std?
  • 1
    Are you sure that `` is your only include? Headers can and do often include other headers which would explain that you see this functions as declared. However, I doubt that `` includes all the examples you mention – 463035818_is_not_an_ai Nov 24 '15 at 17:12
  • With regard to your first question: http://stackoverflow.com/q/17073066/1538531 – Derek Nov 24 '15 at 17:15
  • Doesn't work on MSVC2015 or MinGW-GCC-5. @tobi303 I doubt it too now – Ivan Aksamentov - Drop Nov 24 '15 at 17:16
  • 1
    "C" functions are not "defined as pointers to functions" it's just that when you specify just the name of the function the "C" standard has declared that to mean the address of the function, that is, "div == &div". I don't like it either, but that's the way it is. And since "C++" is based on the "C" standard, that same meaning has carried over. – GreatAndPowerfulOz Nov 24 '15 at 17:18
  • @tobi303 , http://ideone.com/iSitZQ - yes I'm sure iostream is my only include –  Nov 24 '15 at 18:23
  • @Great.And.Powerful.Oz Not sure if I understand you correctly. Does the C standard declare that the name of a function is equivalent to the address of the function? If so, what does e.g. `printf("%d",10)` mean? `0x00ff88dd33("%d",10)`? Doesn't make much sense... –  Nov 24 '15 at 18:29
  • 1
    @gaazkam, yes, that is right the C standard does declare that. So yes,`printf("%d", 10)` is equivalent to a properly casted `0x00ff88dd33("%d",10)` – GreatAndPowerfulOz Nov 26 '15 at 19:32

2 Answers2

4
  • Standard library headers are allowed to include other headers without advertising the fact.
  • These are functions, and their names decay to pointer to function
  • Because the standard unfortunately allows to import names from the C library into the global namespace even if these are defined in one of the <cxxx> headers.
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Nice. But in this case, why does prepending `std::` to the name of the function cause compilation error? Like in `std::cout << std::div`? Why doesn't `std::div` decay to pointer to function? –  Nov 24 '15 at 18:32
  • @gaazkam It should, decay but maybe that name hasn't been introduced by the include. It could be that the implementation is non-conforming. – juanchopanza Nov 24 '15 at 19:46
  • `#include ` (...) `std::cout << (std::div) << '\n';` gives out kilometre-long compilation errors: http://ideone.com/BT8rbL Having looked at them a little more carefully, it seems that `std::div` does decay, only the compiler doesn't know the adress of which overload it should use. –  Nov 24 '15 at 20:03
  • @gaazkam That makes sense, there are a few overloads: http://en.cppreference.com/w/cpp/numeric/math/div – juanchopanza Nov 24 '15 at 21:31
1

try this

//#include <iostream>

int main()
{
  int j =  (div) ;
  return 0;
}

It fails to compile. Somewhere in iostream it is including stdlib.h which defines div into the global namespace

div is a function in your code. Which c++ ends up implicitly converting to a true bool

pm100
  • 48,078
  • 23
  • 82
  • 145