Created
March 23, 2013 13:50
-
-
Save rmartinho/5227803 to your computer and use it in GitHub Desktop.
Using char_traits to get strong string aliases.
This file contains hidden or 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 <string> | |
#include <iostream> | |
namespace dessert { | |
template <typename Tag> | |
struct not_quite_the_same_traits : std::char_traits<char> {}; | |
template <typename Tag> | |
using strong_string_alias = std::basic_string<char, not_quite_the_same_traits<Tag>>; | |
using vanilla_string = std::string; | |
using strawberry_string = strong_string_alias<struct strawberry>; | |
using caramel_string = strong_string_alias<struct caramel>; | |
using chocolate_string = strong_string_alias<struct chocolate>; | |
template <typename T> | |
struct special; | |
template <typename T> | |
using special_string = strong_string_alias<special<T>>; | |
std::ostream& operator<<(std::ostream& os, vanilla_string const& s) { | |
return os << "vanilla: " << s.data(); | |
} | |
std::ostream& operator<<(std::ostream& os, strawberry_string const& s) { | |
return os << "strawberry: " << s.data(); | |
} | |
std::ostream& operator<<(std::ostream& os, caramel_string const& s) { | |
return os << "caramel: " << s.data(); | |
} | |
std::ostream& operator<<(std::ostream& os, chocolate_string const& s) { | |
return os << "chocolate: " << s.data(); | |
} | |
template <typename T> | |
std::ostream& operator<<(std::ostream& os, special_string<T> const& s) { | |
return os << "special: " << s.data(); | |
} | |
} | |
int main() { | |
dessert::vanilla_string vanilla = "foo"; | |
dessert::strawberry_string strawberry = "foo"; | |
dessert::caramel_string caramel = "foo"; | |
dessert::chocolate_string chocolate = "foo"; | |
std::cout << vanilla << '\n'; | |
std::cout << strawberry << '\n'; | |
std::cout << caramel << '\n'; | |
std::cout << chocolate << '\n'; | |
dessert::special_string<struct nuts> nuts = "foo"; | |
std::cout << nuts << '\n'; | |
} |
This file contains hidden or 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
foo | |
strawberry: foo | |
caramel: foo | |
chocolate: foo | |
special: foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment