Skip to content

Instantly share code, notes, and snippets.

@pedropedruzzi
Created February 11, 2017 21:28
Show Gist options
  • Save pedropedruzzi/1faba2a9eca81c588075369f3c824cb1 to your computer and use it in GitHub Desktop.
Save pedropedruzzi/1faba2a9eca81c588075369f3c824cb1 to your computer and use it in GitHub Desktop.
#include <iostream>
#define STRONG_TYPEDEF(original, newtype) \
class newtype : public original \
{ \
using original::original; \
};
using Age = unsigned int;
using Name = std::string;
using Country = std::string;
class Person
{
public:
Person(Age age_, Name name_, Country country_)
: age(age_)
, name(name_)
, country(country_) {}
Age age;
Name name;
Country country;
};
STRONG_TYPEDEF(Person, Brazilian);
STRONG_TYPEDEF(Person, Lithuanian);
bool from_south_america(Person &p){
return p.country == "Brazil";
}
bool from_sao_paulo(Brazilian &b) {
return true;
}
int main() {
Brazilian ricardo(30, "Ricardo", "Brazil");
Lithuanian egle(24, "Egle", "Lithuania");
std::cout << ricardo.name << " is" << (from_south_america(ricardo)? "": " not") << " South American" << std::endl;
std::cout << egle.name << " is" << (from_south_america(egle)? "": " not") << " South American" << std::endl;
std::cout << ricardo.name << " comes from " << (from_sao_paulo(ricardo)? "Sao Paulo" : "elsewhere") << std::endl;
//std::cout << egle.name << " comes from " << (from_sao_paulo(egle)? "Sao Paulo" : "elsewhere") << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment