Posts

Showing posts with the label operators

Multiplying complex with constant in C++

Image
Clash Royale CLAN TAG #URR8PPP Multiplying complex with constant in C++ The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace std; int main(void) { const double b=3; complex <double> i(0, 1), comp; comp = b*i; comp = 3*i; return 0; } with error: no match for ‘operator*’ in ‘3 * i’ What is wrong here, why cannot I multiply with immediate constants? b*i works. 3 Answers 3 In the first line: comp = b*i; The compiler calls: template<class T> complex<T> operator*(const T& val, const complex<T>& rhs); Which is instanced as: template<> complex<double> operator*(const double& val, const complex<double>& rhs); In the second case, there is no appropriate template int , so the instancing fails: int comp = 3.0 * i; // no operator*(int, complex<double>) S...

Is it possible to create a new operator in c#?

Image
Clash Royale CLAN TAG #URR8PPP Is it possible to create a new operator in c#? I know you can overload an existing operator. I want to know if it is possible to create a new operator. Here's my scenario. I want this: var x = (y < z) ? y : z; To be equivalent to this: var x = y <? z; In other words, I would like to create my own <? operator. <? no language have This capability... – AminM Jul 5 '13 at 16:05 @JesonPark - Not true. F# has it as others have pointed out, and C++ has it as well. CoffeeScript offers several new operators as syntactic sugar for JavaScript idioms, and it's so customizable that you could describe it as allowing custom operators. The last one's a bit tricky, since you'd technically be extending the language. – Justin Morgan ...