Skip to content

Instantly share code, notes, and snippets.

@sigman78
Created September 28, 2021 19:26
Show Gist options
  • Save sigman78/723c4e24fa7345b71277841c484a17cc to your computer and use it in GitHub Desktop.
Save sigman78/723c4e24fa7345b71277841c484a17cc to your computer and use it in GitHub Desktop.
Strongeesh typedefs in C++17
#include <iostream>
struct UserName { std::string name; };
struct Password { std::string pwd; };
template<typename T>
auto peek(const T& packed) {
static_assert(std::is_aggregate_v<T>, "Should be struct");
// we can further enforce T to have single field with
const auto& [value] = packed;
return value;
}
void login(UserName name, Password pass) {
std::cout << peek(name) << ":" << peek(pass) << "\n";
}
int main(int argc, char** argv) {
login(UserName{"vlad"}, Password{"abc"});
// Wont compile: login(Password{"abc"}, UserName{"vlad"});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment