How does cmath define its namespaces?

I tend to use 'j0', 'j1', 'j2' as parameters inside my for loops.
Lately, I wrote a function like this:
1
2
3
4
5
6
7
8
#include <cmath>
using std::sqrt

void foo(unsigned int uj1)
{
  if(j1>0)
   //do stuff...
}

Unfortunately, the g++ compiler did compile this piece of code because 'j1' is defined as a Bessel function in 'cmath'. But why is it defined although I don't include the whole namespace?
Last edited on
I have tried to reproduce this and got this:
E:\Users\MiiNiPaa\home\Developement\CodeBlocks\projects\Test\main.cpp|6|error: 'j1' was not declared in this scope|


For me everything in cmath in std namespace.
And even if it was in global, you should write if(j1()>0) to actually call function. Maybe j1 is a global variable somewhere?
closed account (zb0S216C)
a z A Z 0 9 3 24 wrote:
"Unfortunately, the g++ compiler did compile this piece of code because 'j1' is defined as a Bessel function in 'cmath'."

No, the "std" name-space does not have a function called "j1" -- unless your compiler has defined a function call "j1" somewhere inside its implementation. Sometimes, the compiler is able to define functions that are accessible in every scope; "__builtin_...( )" is an example of one of those functions.

Wazzak
Last edited on
Well, this does compile... (g++)

1
2
3
4
5
6
7
8
9
10
#include <cmath>
using std::sqrt;

int main()
{
	int ret_val = 0;
	if(j1) // j1 is not defined here... 
		ret_val++;
	return ret_val;
}


As I just found out that there is an extension to the <cmath> package.

http://en.wikibooks.org/wiki/C_Programming/C_Reference/math.h

The thing is that I never use this stupid Bessel functions nor do I link to any libaries! It seems to be more of a g++ issue. Any hints how I can make sure to use only the standard declarations?


I guess I should close this thread. Thank you for your time :)

Edit: At least I will get a message, if I use the '-Wall' flag.


...$ g++ -Wall -pedantic -ansi wtf.cpp
wtf.cpp: In function ‘int main()’:
wtf.cpp:8:7: warning: the address of ‘double j1(double)’ will always evaluate as ‘true’ [-Waddress]
Last edited on
The wikibooks article states "These are not present in any ANSI or ISO C standard."

Presumably these functions can be enabled or disabled by selecting some particular compiler option.

For example this compiler option, I don't know whether it would work:
-std=c++11
Last edited on
Topic archived. No new replies allowed.