Skip to content

Instantly share code, notes, and snippets.

@bvibber
Created May 16, 2018 19:36
Show Gist options
  • Save bvibber/b41084643cf713e73b0133b9f2a70866 to your computer and use it in GitHub Desktop.
Save bvibber/b41084643cf713e73b0133b9f2a70866 to your computer and use it in GitHub Desktop.
smart pointer overload confusion
#include <iostream>
class Obj;
class Wrapper {
Obj* ptr;
public:
Wrapper(Obj* aptr) : ptr(aptr) {}
Obj& operator*() {
return *ptr;
}
operator const Obj*() {
return ptr;
}
};
class Obj {};
void func(bool abool) {
std::cout << "bool\n";
}
void func(Obj* obj) {
std::cout << "obj\n";
}
int main() {
// calls the obj variant
func(new Obj());
// calls the bool variant
func(Wrapper(new Obj()));
// calls the obj variant
func(&*Wrapper(new Obj()));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment