Skip to content

Instantly share code, notes, and snippets.

@mloskot
Created April 18, 2012 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloskot/2412967 to your computer and use it in GitHub Desktop.
Save mloskot/2412967 to your computer and use it in GitHub Desktop.
Smart pointer example with mis-use of bool conversion
// Interesting issue about conversion operators in C++
//http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1592.pdf
#include <iostream>
template <class T>
class Ptr
{
// stuff
public:
operator bool() const
{
if( rawptr_ )
return true;
else
return false;
}
private:
T * rawptr_;
};
int main()
{
Ptr<int> p1;
Ptr<float> p2;
std::cout << "p1 + p2 = " << p1 + p2 << std::endl; // prints 0, 1, or 2
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment