-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loop over types in the variant? #213
Comments
@Yurlungur this might be a good place to stash that code you had put in our Mattermost convo as well. |
👍 good idea @dholladay00 . Something like this allows us to loop over EOS type. I had it in mind for the rewrite of the EOS builder #28 . #include <iostream>
#include <string>
#include <variant>
#include <type_traits>
#include <tuple>
template <typename... Ts>
struct type_list {};
template <typename... Ts>
struct tl_to_Variant_struct {
using vt = std::variant<Ts...>;
};
template <typename... Ts>
constexpr auto tl_to_variant(type_list<Ts...>) {
return tl_to_Variant_struct<Ts...>{};
}
template<typename T, typename... Ts>
constexpr bool contains(){
return std::disjunction_v<std::is_same<T, Ts>...>;
}
struct Foo {};
struct Bar {};
template<typename T>
struct Mod {
Mod(T t) {};
};
static constexpr const auto base_list = type_list<Foo, Bar>{};
static constexpr const auto mod_list = type_list<Foo, Bar, Mod<Foo>, Mod<Bar>>{};
using Var_t = typename decltype(tl_to_variant(mod_list))::vt;
template<typename Variant, template <class...> typename T>
Variant Modify(Variant var) {
return var;
}
template<typename Variant, template <class...> typename T, typename U, typename... Rest>
Variant Modify(Variant var) {
if (std::holds_alternative<U>(var)) {
return Variant(T<U>(std::get<U>(var)));
} else {
return Modify<Variant,T,Rest...>(var);
}
}
int main() {
Var_t f = Foo();
auto m = Modify<Var_t, Mod, Foo, Bar>(f);
return 0;
} |
Interesting... this does look a lot more extensible than the EOS builder machinery |
I haven't worked out all the kinks yet, but I think it should let us write code like this (I hope): EOS my_eos = IdealGas();
if (shifted) {
my_eos = Modify<Shifted, ... >(my_eos, shift);
}
if (scaled) {
my_eos = Modify<Scaled,...>(my_eos, scale);
}
// etc |
I wonder if it's possible to achieve vectorization (for the EOS's that vectorize) for the scalar calls by looping over EOS type in the variant first. I bet this kind of loop construct can be done at compile time. Not sure this is a priority but I wanted to write the idea down somewhere. Would also like to pick your brain @dholladay00 .
The text was updated successfully, but these errors were encountered: