template-meta-programming

C++ type traits to select between T1 and T2

孤街醉人 提交于 2019-12-06 19:47:59
问题 I want a template to select from two types based on some condition. E.g. struct Base {}; template <typename T1, typename T2> struct test { // e.g. here it should select T1/T2 that is_base_of<Base> typename select_base<T1, T2>::type m_ValueOfBaseType; }; Of course to pass condition to the select_base (to make it generic) would be useful, but hard-coded solution is easier and good as well. Here's a sample solution that I tried but it always selects T1: http://ideone.com/EnVT8 The question is

Searching through a tuple for arguments of a function

天涯浪子 提交于 2019-12-06 14:56:44
Consider this int foo (int a, char c, bool b) {std::cout << a << ' ' << c << ' ' << b << '\n'; return 8;} double bar (int a, char c, bool b, int d) {std::cout << a << ' ' << c << ' ' << b << ' ' << d << '\n'; return 2.5;} char baz (bool a, bool b) {std::cout << a << ' ' << b << '\n'; return 'a';} int main() { const auto tuple = std::make_tuple(5, true, 'a', 3.5, false, 1000, 't', 2, true, 5.8); const std::tuple<int, double, char> t = searchArguments (tuple, foo, bar, baz); } So the arguments for foo are first searched (from tuple ). Searching from left to right, the first int found is 5 , the

Get the subset of a parameter pack based on a given set of indices

做~自己de王妃 提交于 2019-12-06 12:06:07
Okay, this is a really difficult one. I want to be able to get the subset of a parameter pack by choosing the parameter types at a given set of valid indices and then use that parameter pack as the argument list of a function. IE: template <size_t... indices_t> void func(pack_subset<indices_t..., args_t...>); //incorrect syntax, //not sure how else to write this //args_t is predetermined, this //function is a static member of //a variadic class template //For a parameter pack <float, double> and index set 0 void func(float); // <- result of template I can get the type of a single index, but a

Mixing aliases and template specializations

扶醉桌前 提交于 2019-12-06 11:48:15
问题 I'm trying to find the best method to have a kind of "object" that can be either specialized or "linked" to another type. For instance you cannot specialize a class to make it become a simple int, and you cannot use the keyword using to specialize classes. My solution is the following: template<class Category, Category code> struct AImpl {}; template<class Category, Category code> struct AHelper { using type = AImpl<Category, code>; }; template<class Category, Category code> using A =

How can I arbitrarily sort a tuple's types?

本小妞迷上赌 提交于 2019-12-06 09:07:45
One thing that really annoys me about C++ is that an empty struct / class takes up space. So, I have this idea that std::tuple (or some variant, since it's (and the compiler's) implementation is highly implementation dependent) might be able to save the day, which it sort of does, but there are issues due to packing and alignment. Because of how compilers will align the items in the struct , having a empty next to a non-empty next to an empty next to a non-empty will be larger than 2 empties next to 2 non-empties. Because of this, I need a way to reorder the types based on some criteria.

Generically call member function on each element of a tuple

时光总嘲笑我的痴心妄想 提交于 2019-12-06 08:20:37
问题 Step one: expand a tuple and pass elements to a function: I have a function which takes N parameters void func(int, double, char); and a tuple with the matching types std::tuple<int, double, char> tuple; As per this stackoverflow question, I am able to expand the tuple and call the function. Step two: expand a tuple and pass result of calling a member function on each of the elements to a function: Taking it a step further, my tuple contains multiple instances of a class template: template

Perfect forwarding of functions to build a function list class

孤者浪人 提交于 2019-12-06 07:25:43
问题 Consider the following code that build a class storing functions. // Function list class template <class... F> struct function_list { template <class... G> constexpr function_list(G&&... g) noexcept : _f{std::forward<G>(g)...} { } std::tuple</* F... OR F&&... */> _f; }; // Function list maker template <class... F, class R = /* Can we compute the return type here? */> constexpr R make_function_list(F&&... f) { return function_list< /* decltype(std::forward<F>(f))... * OR F... * OR F&&... */>

How to define a variant<x,y,z> extracting subtypes of a template parameter

风流意气都作罢 提交于 2019-12-06 06:42:13
问题 I am building a state-machine where state transitions are described as a variant, i.e.: using table = std::variant< /* state event followup-state */ transition<start, success<sock>, connecting>, transition<start, exception, failed>, transition<connecting, success<>, connected>, transition<connecting, exception, failed>, transition<connected, exception, failed> >; and transition being a simple type: template <typename ENTRY_STATE, typename EVENT, typename NEXT_STATE> struct transition { using

boost.proto + modify expression tree in place

一个人想着一个人 提交于 2019-12-06 03:53:31
问题 Background question: boost.proto + detect invalid terminal before building the expression tree. Hi, what i'm trying to achieve is create a copy of an expression tree, where all vectors are substituted with their begin iterators (in my case is a raw pointer) increment the iterators in place dereference iterators in the tree, but that part should be relatively easy. So, for 1. I ended up with this code /////////////////////////////////////////////////////////////////////////////// // A

Boost MPL: Call a (member) function only if it exists

拈花ヽ惹草 提交于 2019-12-06 02:44:25
问题 I have a class A that has a template parameter T. There are use cases where the class T offers a function func1() and there are use cases where T doesn't offer it. A function f() in A should call func1(), iff it exists. I think this should be possible with boost mpl, but I don't know how. Here some pseudo code: template<class T> class A { void f(T param) { if(T::func1 is an existing function) param.func1(); } }; Even better would be an else-case: template<class T> class A { void f(T param) {