Created
May 16, 2018 19:36
-
-
Save bvibber/b41084643cf713e73b0133b9f2a70866 to your computer and use it in GitHub Desktop.
smart pointer overload confusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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