Voko
Voko

Reputation: 778

C++ generate function with template metaprogramming

How can I generate a function using template metaprogramming. What I want to do is have a bunch of functions that basically do the same thing:

Type1 fun1(int arg1, int arg2) {
  Type1 newType1 = {};
  newType1.arg1 = arg1;
  newType1.arg2 = arg2;

  return newType1;
}

Type2 fun2(int arg1, int arg2, int arg3, bool arg4) {
  Type2 newType2 = {};
  newType2.arg1 = arg1;
  newType2.arg2 = arg2;
  newType2.arg3 = arg3;
  newType2.arg4 = arg4;

  return newType2;
}

So basically I don't want to write all these functions myself, I want for example say that I want a function fun1 that takes two int arguments and assign them to a new object of Type1 using templates but how?

My idea is to have a template function that takes a type (here Type1 or Type2) and pointers-to-members of these types, so the only thing I have to do is give the template the pointers-to-members and it generates the function that takes arguments of the corresponding type.

Upvotes: 2

Views: 2219

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275310

This is a answer:

template<auto PMem>
struct member_type {};
template<class T, class M, M(T::*ptr)>
struct member_type<ptr> { using type=M; };
template<auto PMem>
using member_type_t=typename member_type<PMem>::type;

template<class T, auto...PMem>
T func( member_type_t<PMem>... args ) {
  T retval = {};
  ( ((retval.*PMem) = std::forward<member_type_t<PMem>>(args)), ... );
  return retval;
}

test code:

struct Bob {
  int x,y;
};

int main() {
    Bob b = func<Bob, &Bob::x, &Bob::y>( 2, 3 );
    (void)b;
}

live example.

You can also perfect forward without matching types. This has the downside that this doesn't work:

struct A {
  int x, y;
};
struct B {
  A one, two;
};
B func<B, &B::one, &B::two>( {1,2}, {3,4} );

but it does eliminate some boilerplate above, and it could remove a redundant move per member field.

To do that, simply drop the member_type helper completely:

template<class T, auto...PMem, class...Args>
T func( Args&&... args ) {
  T retval = {};
  ( ((retval.*PMem) = std::forward<Args>(args)), ... );
  return retval;
}

Doing this outside of is a pain. You lack auto parameters and ... expansion of statements. The second is relatively easy to work around with some boilerplate, but the first makes your desired syntax basically impossible; you may be reduced to using macros.

If you don't want <> syntax:

template<class T, auto...PMem>
constexpr auto make_func() {
  return +[]( member_type_t<PMem>... args )->T {
    T retval = {};
    ( ((retval.*PMem) = std::forward<member_type_t<PMem>>(args)), ... );
    return retval;
  };
}

struct Bob {
  int x,y;
};

constexpr auto* func = make_func<Bob, &Bob::x, &Bob::y>();    

Live example.

A constexpr function pointer should be treated nearly indistinguishably from a function, except overloading isn't available.

In MSVC you might have to disambiguate the function pointer type like this:

template<class T, auto...PMem>
using func_t = T(*)(member_type_t<PMem>...);

template<class T, auto...PMem>
constexpr func_t<T, PMem...> make_func() {
  return []( member_type_t<PMem>... args )->T {
    T retval = {};
    ( ((retval.*PMem) = std::forward<member_type_t<PMem>>(args)), ... );
    return retval;
  };
}

Live example.

Sometimes MSVC has problems with unary operator + on stateless lambdas having multiple different calling convention optoins. The above avoids that issue, at the cost of a bit of boilerplate.

Upvotes: 4

Related Questions