The problem is your NotAString(std::string str)
constuctor is not explicit
so it allows implicit conversions from std::string
to NotAString
.
When you call the function with a std::string
the compiler notices that you could call it by converting the argument via the constructor, so it creates a NotAString
temporary and passes it to the function.
If you declare it explicit NotAString(std::string str)
then it won't allow those implicit conversions.
Your attempts to check the type inside the function are never going to work, by that point the compiler has create a NotAString
and all you're testing is whether a NotAString
argument is not a NotAString
... which is obviously not going to work.