Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Runtime index for choosing type
// compile: clang++ -Wall -std=c++14 -I /usr/local/include mp11-type-list-switch.cpp -o mp11-type-list-switch
#include <vector>
#include <iostream>
#include <boost/mp11/list.hpp>
#include <boost/mp11/algorithm.hpp>
namespace mp11 = boost::mp11;
using num_types = mp11::mp_list<int, double>;
template <size_t i>
using num_type = mp11::mp_at_c<num_types, i>
template<class F>
inline constexpr
decltype(std::declval<F>()(std::declval<mp11::mp_size_t<0>>()))
with_type(std::size_t i, F && f) {
return mp11::mp_with_index< mp11::mp_size<num_types> >(i,
std::forward<F>(f));
}
size_t length(size_t i, size_t len) {
return with_type(i, [&](auto I) {
std::vector< num_type<I> > vec(len, 100);
return vec.size();
});
}
int main () {
std::cout << "length: " << length(0, 4) << "\n";
std::cout << "length: " << length(1, 5) << "\n";
std::cout << "length: " << length(2, 6) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.