Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Created October 17, 2014 03:08
Show Gist options
  • Save Fiona-J-W/6e256eac0abd4d75abef to your computer and use it in GitHub Desktop.
Save Fiona-J-W/6e256eac0abd4d75abef to your computer and use it in GitHub Desktop.
Named arguments
#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