help me to converting a char (-) into an operator (+)

Pages: 12
Cmon guyz, any ideas at all? I'm bored!!
Come ON!!!

@viliml
No, it shall not! I tried it out with this example:cout<<char_to_op<int>('^')(3, 2);
Do you have any examples that really DON'T work?



Here is a simple example

1
2
3
4
5
6
7
8
#include <cmath>

int main()
{
   int x = 2, y = 3;

   std:pow( x, y );
}



Last edited on
sure, that doesn't work, but when its used in my function, it works!
@viliml
sure, that doesn't work, but when its used in my function, it works!


It is implementation-defined behaviour and other compilers can do not compile this code. Your compiler compilers this code because it places C-standard functions in the global namespace and you use pow without nested name std::. However as I have said it is not obligatory that an implementation shall place C-standard functions in the global namespace.
Last edited on
No, it doesn't put it in then global namespace! I put using namespace std; at the top of every code. :D
that's what puts it in the global namespace
Oh.
@viliml
No, it doesn't put it in then global namespace! I put using namespace std; at the top of every code. :D


In this case your code shall ot be compiled if you will use objects of type int because the compiler will not know which overloaded function pow to use.


Really? It works every time on my comp(which can be either computer or compiler)!Try this out:
1
2
3
cout<<char_to_op<int>('^')(1, 2)<<endl;
int x=char_to_op<int>('^')(3, 4);
if (char_to_op<int>('^')(6, 5)==7776) cout<<"See? It works!"<<endl;

It works on my comp, let's see if it's implementation-specific.
bump
When please answer which pow function shall the compiler select? It has three functions

float pow( float, float );
double pow( double, double );
long double pow( long double, long double );

I think your code works due to a bug of the compiler you are using.
Last edited on
Anything from the OP yet?
Nope. Not a word...
Topic archived. No new replies allowed.
Pages: 12