Created
September 28, 2021 19:26
-
-
Save sigman78/723c4e24fa7345b71277841c484a17cc to your computer and use it in GitHub Desktop.
Strongeesh typedefs in C++17
This file contains 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 <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