GCC can’t differentiate between operator++() and operator++(int)

Name lookup must occur first. In this case for the name operator++. [basic.lookup] (emphasis mine) 1 The name lookup rules apply uniformly to all names (including typedef-names ([dcl.typedef]), namespace-names ([basic.namespace]), and class-names ([class.name])) wherever the grammar allows such names in the context discussed by a particular rule. Name lookup associates the use of a name … Read more

Groovy different results on using equals() and == on a GStringImpl

Nice question, the surprising thing about the code above is that println “${‘test’}”.equals(‘test’) returns false. The other line of code returns the expected result, so let’s forget about that. Summary “${‘test’}”.equals(‘test’) The object that equals is called on is of type GStringImpl whereas ‘test’ is of type String, so they are not considered equal. But … Read more

Can I use ‘ == ‘ to compare two vectors. I tried it and seems to be working fine. But I don’t know whether it will work in more complex situations

The overload of operator == that works on two std::vectors will compare the vector sizes and return false if those are different; if not, it will compare the contents of the vector element-by-element. If operator == is defined for the vector’s element type, then the comparison of vectors through operator == is valid and meaningful. … Read more

De Morgan’s Law optimization with overloaded operators

Note that: Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands. … Because the short-circuiting properties of operator&& and operator|| do not apply to overloads, and because types … Read more

std::endl is of unknown type when overloading operator

std::endl is a function and std::cout utilizes it by implementing operator<< to take a function pointer with the same signature as std::endl. In there, it calls the function, and forwards the return value. Here is a code example: #include <iostream> struct MyStream { template <typename T> MyStream& operator<<(const T& x) { std::cout << x; return … Read more

Defining operator< for a struct

This is quite an old question and as a consequence all answers here are obsolete. C++11 allows a more elegant and efficient solution: bool operator <(const MyStruct& x, const MyStruct& y) { return std::tie(x.a, x.b, x.c) < std::tie(y.a, y.b, y.c); } Why is this better than using boost::make_tuple? Because make_tuple will create copies of all … Read more

How should I write ISO C++ Standard conformant custom new and delete operators?

Part I This C++ FAQ entry explained why one might want to overload new and delete operators for one’s own class. This present FAQ tries to explain how one does so in a standard-conforming way. Implementing a custom new operator The C++ standard (ยง18.4.1.1) defines operator new as: void* operator new (std::size_t size) throw (std::bad_alloc); … Read more