Last active
April 28, 2025 01:40
-
-
Save dk949/90f6744785d9dbc64f4373b1ce463153 to your computer and use it in GitHub Desktop.
for loop, break, continue and indexing for template parameter packs
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
#ifndef UT_PACK_LOOPS_HPP | |
#define UT_PACK_LOOPS_HPP | |
/* Usage: | |
* std >= c++17 (c++20) | |
* | |
* XXX: **DO NOT RETURN FROM UT_PACK_FOR** it uses a lambda so you would only return from that lambda. | |
* If you do want to break the loop, use UT_PACK_BREAK instead of returning. | |
* | |
* template<std::size_t... sizes> | |
* std::size_t foo() { | |
* std::size_t sum = 0; | |
* | |
* UT_PACK_FOR(std::size_t size, sizes, { | |
* sum += size; | |
* std::printf("added %lu\n", size); | |
* if (sum >= 100) { | |
* std::puts("sum too big"); | |
* UT_PACK_BREAK; | |
* } | |
* }); | |
* return sum; | |
* } | |
* | |
* template<typename... Args> | |
* std::string bar(Args &&...args) { | |
* std::string out; | |
* UT_PACK_FOR(auto &&var, args, { | |
* using T = std::decay_t<decltype(var)>; | |
* std::string add; // NOTE: this is contrived to show UT_PACK_CONTINUE | |
* if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>) { | |
* add = std::to_string(var); | |
* } else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, char const *>) { | |
* add = var; | |
* } else { | |
* std::puts("cannot add this type to the string"); | |
* UT_PACK_CONTINUE; | |
* } | |
* | |
* std::printf("added %s\n", add.c_str()); | |
* out += std::move(add); | |
* }); | |
* return out; | |
* } | |
* struct A { static constexpr auto a = 1; }; | |
* struct B { static constexpr auto b = 2; }; | |
* struct C { static constexpr auto c = 3; }; | |
* template<typename... Ts> | |
* int baz() { | |
* int val = 0; | |
* UT_PACK_FOR_T(I, Ts, { | |
* if constexpr (requires { I::b; }) { | |
* val = I::b; | |
* UT_PACK_BREAK; | |
* } | |
* }); | |
* return val; | |
* } | |
* | |
* template<typename... Ts> | |
* int get_2nd_plus_1(Ts &&...args) { | |
* ++UT_PACK_IDX_MUT(args, 1); | |
* return UT_PACK_IDX_MUT(args, 1); | |
* } | |
* | |
* template<int... ints> | |
* int get_1st() { | |
* static_assert(sizeof...(ints) == 2); | |
* return UT_PACK_IDX(ints, 0); | |
* } | |
* | |
* int main() { | |
* foo<1, 2, 3, 100, 4>(); | |
* std::puts("________"); | |
* std::puts(bar(1, 2.3, nullptr, "hello").c_str()); | |
* #if __cplusplus >= 202002 | |
* std::printf("the `b` member is %d\n", baz<A, B, C>()); | |
* #endif | |
* std::printf("%d, %d\n", get_1st<1, 2>(), get_2nd_plus_1(1, 2)); | |
* } | |
* // output: | |
* // added 1 | |
* // added 2 | |
* // added 3 | |
* // added 100 | |
* // sum too big | |
* // ________ | |
* // added 1 | |
* // added 2.300000 | |
* // cannot add this type to the string | |
* // added hello | |
* // 12.300000hello | |
* // the `b` member is 2 | |
* // 1, 3 | |
*/ | |
#define UT_PACK_FOR(var, pack, stmt) \ | |
void(([&](var) -> bool { \ | |
{ stmt } \ | |
return true; \ | |
}(pack) && ...)) | |
#if __cplusplus >= 202'002 | |
/// Iterate type types in `Pack` using `Var` as the placeholder | |
# define UT_PACK_FOR_T(Var, Pack, stmt) \ | |
void(([&]() -> bool { \ | |
using Var = Pack; \ | |
{ stmt } \ | |
return true; \ | |
}() && ...)) | |
#endif | |
#define UT_PACK_BREAK return false | |
#define UT_PACK_CONTINUE return true | |
/// Can be used for both argument pack and template parameter pack, but returns a const ref | |
#define UT_PACK_IDX(pack, idx) \ | |
std::get<idx>(std::tuple<std::add_lvalue_reference_t<std::add_const_t<decltype(pack)>>...> {pack...}) | |
/// Returns a mutable ref, but can only be used for argument pack, not template parameter pack (since it's always const) | |
#define UT_PACK_IDX_MUT(pack, idx) std::get<idx>(std::tuple<std::add_lvalue_reference_t<decltype(pack)>...> {pack...}) | |
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
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 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. | |
For more information, please refer to <https://unlicense.org> | |
*/ | |
#endif // UT_PACK_LOOPS_HPP |
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
loop over variadic template parameters (either values or types) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment