It's possible to use an alias to change the literal signature of a function:
using String = std::string;
void Print(String s) { ... };
But this doesn't disallow calling Print
with a std::string
:
Print(std::string{"Hello world"}); // Still works
This makes sense -- an alias is strictly for simplifying a name for a type, it does not define a new type.
Besides subclassing, which is not a good idea, is there a mechanism by which it's possible to achieve strict typing by name for function parameters? An immediate consequence of this is that this would be possible as well:
using StringA = std::string;
using StringB = std::string;
void Print(StringA s) { ... };
void Print(StringB s) { ... };
Print(StringA{"Hello world"});
Print(StringB{"Hi everyone"});
My current solution is to define simple wrapper classes that hold the type I want to alias as a member. This isn't ideal because it requires duplicating the interface for the member class into the wrapper, something that isn't necessary when using an alias.