Created
October 17, 2014 03:08
-
-
Save Fiona-J-W/6e256eac0abd4d75abef to your computer and use it in GitHub Desktop.
Named arguments
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 <algorithm> | |
#include <cstdint> | |
#include <experimental/string_view> // already works in g++ 4.9.1 | |
//format("{bla}, {blub}", "bla"_id = 3, "blub"_id = "foobar"); | |
using std::experimental::string_view; | |
template<typename T> | |
struct id { | |
id(string_view name, const T& data): name{name}, data{data} {} | |
string_view name; | |
const T& data; | |
}; | |
struct id_base { | |
string_view name; | |
template<typename Arg> | |
id<Arg> operator=(const Arg& arg) const { | |
return id<Arg>{name, arg}; | |
} | |
}; | |
id_base operator"" _id(const char* str, std::size_t len) { | |
return id_base{{str, len}}; | |
} | |
int main() { | |
auto foo = ("abc"_id = 37); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment