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

Pages: 12
Aug 2, 2012 at 2:25pm
Cmon guyz, any ideas at all? I'm bored!!
Aug 2, 2012 at 2:49pm
Come ON!!!
Aug 2, 2012 at 2:50pm

@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 Aug 2, 2012 at 2:51pm
Aug 2, 2012 at 7:43pm
sure, that doesn't work, but when its used in my function, it works!
Aug 2, 2012 at 8:15pm
@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 Aug 2, 2012 at 8:16pm
Aug 3, 2012 at 9:22am
No, it doesn't put it in then global namespace! I put using namespace std; at the top of every code. :D
Aug 3, 2012 at 9:35am
that's what puts it in the global namespace
Aug 3, 2012 at 9:47am
Oh.
Aug 3, 2012 at 12:03pm
@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.


Aug 3, 2012 at 12:27pm
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.
Aug 3, 2012 at 2:02pm
bump
Aug 3, 2012 at 2:17pm
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 Aug 3, 2012 at 2:17pm
Aug 3, 2012 at 2:20pm
Anything from the OP yet?
Aug 3, 2012 at 2:39pm
Nope. Not a word...
Topic archived. No new replies allowed.
Pages: 12