Created
March 7, 2023 15:47
-
-
Save jeaiii/127e5afad3b80cb8867154277759025f to your computer and use it in GitHub Desktop.
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
// https://godbolt.org/z/s9GheEWav | |
// MIT License - Copyright(c) 2023 James Edward Anhalt III - https://github.com/jeaiii | |
#define CPP17 0 | |
#define CPP14 0 | |
#if CPP17 | |
#define AUTO_T auto | |
#else | |
#define AUTO_T unsigned long long | |
#endif | |
template<AUTO_T M, class T = int> struct arg | |
{ | |
static constexpr auto m = M; | |
T n; | |
template<class U> arg<m, U const&> operator=(U const& u) const { return { u }; } | |
}; | |
#if CPP14 | |
template<AUTO_T M, class U> constexpr auto as_member_p = 0; | |
#else | |
template<AUTO_T M, class U> struct as_member_ptr; | |
#endif | |
template<class U, class...Ts> U construct(Ts...ts) | |
{ | |
U u{ }; | |
#if CPP17 | |
((u.*as_member_p<ts.m, U> = ts.n), ...); | |
#elif CPP14 | |
int _z[]{ 0, (u.*as_member_p<ts.m, U> = ts.n, 0)... }; (void)_z; | |
#else | |
int _z[]{ 0, (u.*as_member_ptr<ts.m, U>::m = ts.n, 0)... }; (void)_z; | |
#endif | |
return u; | |
} | |
constexpr unsigned long long hash(char const text[], unsigned long long h = 0) | |
{ | |
return text[0] == '\0' ? h : hash(text + 1, ((h >> 5) | (h << (sizeof(h) * 8 - 5))) ^ (unsigned char)*text); | |
} | |
// do this once for all unique member names in any struct | |
constexpr auto $x = arg<hash("x")>{ }; | |
constexpr auto $y = arg<hash("y")>{ }; | |
#if CPP14 | |
template<class U> constexpr auto as_member_p<$x.m, U> = &U::x; | |
template<class U> constexpr auto as_member_p<$y.m, U> = &U::y; | |
#else | |
template<class U> struct as_member_ptr<$x.m, U> { static constexpr auto m = &U::x; }; | |
template<class U> struct as_member_ptr<$y.m, U> { static constexpr auto m = &U::y; }; | |
#endif | |
struct vec2 { int x; int y; }; | |
vec2 test() | |
{ | |
return construct<vec2>($x = 42, $y = 32); | |
} | |
vec2 test2() | |
{ | |
return construct<vec2>($y = 32, $x = 42); | |
} | |
/* | |
vec2 test3() | |
{ | |
return vec2{ .x = 42, .y = 32 }; | |
} | |
*/ | |
/* | |
MIT License | |
Copyright(c) 2023 James Edward Anhalt III - https://github.com/jeaiii | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment