template<typename... Args>
class SomeClass
{
using tuple_type = std::tuple<Args...>; // (ie: <bool,int,bool>)
tuple_type mytuple;
};
template<typename T, typename C, typename... I> // T is SomeClass
class SomeOtherClass
{
void fn(void(C::*f)(bool,int,bool)); // I want this
// based on the T::tuple_type but I'm not sure how.
};
I could simply use tuple_element 3 times if i knew the tuple has 3 elements only, but I don't know that.
Write a generic type trait:
template <class C, class F>
struct mem_ptr;
template <class C, class F>
using mem_ptr_t = typename mem_ptr<C, F>::type;
And specialize it for tuple:
template <class C, class... Args>
struct mem_ptr<C, std::tuple<Args...>> {
using type = void (C::*)(Args...);
};
And then use it:
void fun(mem_ptr_t<C, typename T::tuple_type> f);
This assumes you want void as the return type.
Could generalize this to splitting up the mem_ptr part from the tuple to func part:
template <class C, class F>
struct mem_ptr {
using type = F C::*;
};
template <class C, class F>
using mem_ptr_t = typename mem_ptr<C, F>::type;
template <class R, class T>
struct tuple_to_func;
template <class R, class... Args>
struct tuple_to_func<R, std::tuple<Args...>> {
using type = R(Args...);
};
template <class R, class T>
using tuple_to_func_t = typename tuple_to_func<R, T>::type;
In which case you'd want:
void fun(mem_ptr_t<C,
tuple_to_func_t<void, typename T::tuple_type>
> f);
Related
I am trying to create an IoC Container in C++ that resolves dependencies automatically.
For that I created a function with two variadic parameter packs that is declared like this:
template <class T, typename ... TDependencies, typename... TArgs>
void Register(std::function<std::shared_ptr<T> (std::shared_ptr<TDependencies> ...,
TArgs ...)> && pFactory)
Apparently, it seems the compiler is unable to match this when supplied with
Register<Foo, Bar>(std::function<std::shared_ptr<Foo>(std::shared_ptr<Bar>)>(
[](std::shared_ptr<Bar> bar){return std::make_shared<Foo>(bar);}));
The compile errors say
note: candidate: 'void Container::Register(std::function<std::shared_ptr<_Tp>
(std::shared_ptr<TDependencies>..., TArgs ...)>&&)
[with T = Foo; TDependencies = {Bar}; TArgs = {std::shared_ptr<Bar>}]'
Apparently, it matches std::shared_ptr<Bar> twice. How can I get the compiler not to match the shared_ptr in TArgs too?
Rather than trying to deduce TDependencies directly from the pFactory parameter type, I'd write a type trait to get the dependencies from the whole parameter pack instead. With boost::mp11:
template <class>
struct is_shared_ptr : std::false_type {};
template <class T>
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
namespace mp11 = ::boost::mp11;
template <class... Ts>
using register_traits = mp11::mp_partition<mp11::mp_list<Ts...>, is_shared_ptr>;
template <class T, class F, class... TDependencies, class... TArgs>
void RegisterImpl(F && pFactory,
mp11::mp_list<
mp11::mp_list<std::shared_ptr<TDependencies>...>,
mp11::mp_list<TArgs...>>);
template <class T, class... Ts>
void Register(std::function<std::shared_ptr<T> (Ts...)> && pFactory)
{
return RegisterImpl<T>(
std::forward<std::function<std::shared_ptr<T> (Ts...)>>(pFactory),
register_traits<Ts...>{});
}
And to call it:
Register(std::function{[] (std::shared_ptr<Bar> bar) {
return std::make_shared<Foo>(bar);
}});
Try it on godbolt.org.
If boost::mp11 is not an option, here's how you can implement your own partition template metafunction:
template <class...>
struct list {};
namespace detail {
template <class L, template <class...> class P, class T, class F, class = void>
struct partition;
template <class Next, class... Ls,
template <class...> class P, class T, class... Fs>
struct partition<list<Next, Ls...>, P, T, list<Fs...>,
std::enable_if_t<!P<Next>::value>> :
partition<list<Ls...>, P, T, list<Fs..., Next>> {};
template <class Next, class... Ls,
template <class...> class P, class... Ts, class F>
struct partition<list<Next, Ls...>, P, list<Ts...>, F,
std::enable_if_t<P<Next>::value>> :
partition<list<Ls...>, P, list<Ts..., Next>, F> {};
template <template <class...> class P, class T, class F>
struct partition<list<>, P, T, F> { using type = list<T, F>; };
} // namespace detail
template <class L, template <class...> class P>
using partition = typename detail::partition<L, P, list<>, list<>>::type;
template <class... Ts>
using register_traits = partition<list<Ts...>, is_shared_ptr>;
template <class T, class F, class... TDependencies, class... TArgs>
void RegisterImpl(F && pFactory,
list<list<std::shared_ptr<TDependencies>...>, list<TArgs...>>);
Try it on godbolt.org.
The rest of the code will remain the same.
I'm doing some metaprogramming and I have ran into the following problem:
I have a class that takes one template parameter T, T can be assumed to be a function with an arbitary signature. The class a member variable V, that should have the type std::tuple<> if T takes no arguments or the first argument is not a std::tuple. If the first argument is an std::tuple, V should instead have the same type as first argument.
Example:
void f() // Should resolve to std::tuple<>
void f(int) // Should resolve to std::tuple<>
void f(std::tuple<int, float>) // Should resolve to std::tuple<int, float>
void f(std::tuple<float>, int) // Should resolve to std::tuple<float>
I have been trying something similar to this, but with no success. As it fails when indexing the first arguement on the argument free function, without selecting any of the other alternatives in spite of those being available. I'm using MSVC 2019 16.8.4
#include <functional>
#include <concepts>
namespace detail
{
template<typename... ArgTs>
struct HasArgs : public std::conditional<(sizeof... (ArgTs) > 0), std::true_type, std::false_type>::type {};
}
//!
//! Provides argument function information
//! Based on: https://stackoverflow.com/a/9065203
//!
template<typename T>
class FunctionTraits;
template<typename R, typename... Args>
class FunctionTraits<R(Args...)>
{
public:
static const size_t arg_count = sizeof...(Args);
using HasArguments = detail::HasArgs<Args...>;
using ReturnType = R;
using ArgTypes = std::tuple<Args...>;
template <size_t i>
struct arg
{
using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
};
};
namespace detail
{
template <typename T>
struct is_tuple : std::false_type {};
template <typename... Args>
struct is_tuple<std::tuple<Args...>>: std::true_type {};
}
template <typename T>
concept is_tuple = requires() { detail::is_tuple<T>::value; };
class TestMemberFunctions
{
public:
static int test_f1(std::tuple<int, float>, int)
{
return 0;
}
static int test_f2(int)
{
return 0;
}
static int test_f3()
{
return 0;
}
};
template <typename CreateT> requires (!FunctionTraits<CreateT>::HasArguments::value)
std::tuple<> TypeDeductionDummyFunction();
template <typename CreateT> requires FunctionTraits<CreateT>::HasArguments::value
auto TypeDeductionDummyFunction() -> std::conditional<is_tuple<typename FunctionTraits<CreateT>::template arg<0>::type>,
typename FunctionTraits<CreateT>::template arg<0>::type,
std::tuple<>>;
template <typename T>
class SampleClass
{
decltype(TypeDeductionDummyFunction<T>()) m_member;
};
SampleClass<decltype(TestMemberFunctions::test_f1)> c1;
SampleClass<decltype(TestMemberFunctions::test_f2)> c2;
SampleClass<decltype(TestMemberFunctions::test_f3)> c3;
Something along these lines, perhaps:
template <typename T> struct ExtractFirstTuple;
template <typename R>
struct ExtractFirstTuple<R()> {
using type = std::tuple<>;
};
template <typename R, typename... Ts, typename... Args>
struct ExtractFirstTuple<R(std::tuple<Ts...>, Args...)> {
using type = std::tuple<Ts...>;
};
template <typename R, typename First, typename... Args>
struct ExtractFirstTuple<R(First, Args...)> {
using type = std::tuple<>;
};
Demo
An attempt to build what you want from more primitive operations.
template<typename T, std::size_t N>
struct FunctionArgument {
static constexpr bool exists = false;
};
template<typename R, typename A0, typename... Args>
struct FunctionArgument<R(A0, Args...), 0>{
using type=A0;
static constexpr bool exists = true;
};
template<typename R, typename A0, typename... Args, std::size_t N>
struct FunctionArgument<R(A0, Args...), N>:
FunctionArgument<R(Args...), N-1>
{};
template<class Sig, std::size_t N>
using FuncArg_type = typename FunctionArgument<Sig, N>::type;
template<class Sig, std::size_t N>
constexpr bool FuncArg_exists = FunctionArgument<Sig, N>::exists;
template<class Sig, class Otherwise>
using FirstArgIfExists =
typename std::conditional_t<
FuncArg_exists<Sig,0>,
FunctionArgument<Sig, 0>,
std::type_identity<Otherwise>
>::type;
template<class T, class Otherwise>
struct TypeIfTuple {
using type=Otherwise;
};
template<class...Ts, class Otherwise>
struct TypeIfTuple<std::tuple<Ts...>, Otherwise> {
using type=std::tuple<Ts...>;
};
template<class T, class Otherwise>
using TypeIfTuple_t = typename TypeIfTuple<T,Otherwise>::type;
template<class Sig>
using TheTypeYouWant = TypeIfTuple_t<
FirstArgIfExists<Sig, std::tuple<>>,
std::tuple<>
>;
Using the following metafunction front to get the first type of a typelist I try to write a similar metafunction to extract the first template of a list of templates.
namescpace detail {
template<typename L> struct front_impl;
template<template<typename, typename...> typename L, typename F, typename... I>
struct front_impl<L<F, I...>> {
typedef F type;
};
}
template<typename L> struct front_impl;
template<template<typename, typename...> typename L, typename F, typename... I>
struct front_impl<L<F, I...>> {
typedef F type;
};
One can use this as follows:
template<typename... T>
struct List {
inline static constexpr size_t size = sizeof...(T);
};
using l1 = List<A, B, C>;
using f1 = front<l1>;
Now I try to do the same with a list of templates. Therefore I use a list of templates TList:
template<template<typename> typename... TT>
struct TList {
inline static constexpr size_t size = sizeof...(TT);
};
and the metafunction tfront:
template<typename T> struct tfront;
template<template<typename> typename F, template<typename> typename... R>
struct tfront<TList<F, R...>> {
template<typename T> using type = F<T>;
};
Then I can extract the first of a list of templates A, B, ... (not shown here):
using tlist = TList<A, B, C>;
template<typename T>
using f = typename tfront<tlist>::template type<T>;
f<int> xx;
Then xx ist of type A<int>.
The Question is: can I write the metafunction tfront in the same way as front, that is not as a partial specialization for the list of templates TList but for every variadic template of templates? So I would like to introduce a parameter L for tfront as a template-template-parameter with a variadic list of templates, so that the compiler must also deduce the type of the type L as in the case of front.
I would like to write something like (but what to use as ???):
template<template<typename> typename F, ??? TL, template<typename> typename... R>
struct tfront<TL<F, R...>> {
template<typename T> using type = F<T>;
};
You need an extra layer of template<typename> typename...:
template<typename>
struct tfront;
template<template<typename> typename F,
template<template<typename> typename...> typename TL,
template<typename> typename... R>
struct tfront<TL<F, R...>>
{
};
live example on wandbox
Is anyone knows how to declare generalized template form for the next template specialization:
template <template <class> class Container,
class Value,
class Return,
Return (Container<Value>::*Apply)(const Value &)>
class Example<Container<Value>, Apply>
{
};
Apply must be a pointer to a member function whoes signature is unknown in template declaration.
Do you mean something like this?
template<typename T, typename F>
struct S;
template<template <typename...> class C, typename R, typename... A>
struct S<C<A...>, R(A...)> {
using Apply = R(C<A...>::*)(A...);
// ...
};
As an example:
template<typename... U>
struct T {
int f(int, char) { return 42; }
};
template<typename T, typename F>
struct S;
template<template <typename...> class C, typename R, typename... A>
struct S<C<A...>, R(A...)> {
using Apply = R(C<A...>::*)(A...);
// ...
};
int main() {
S<T<int, char>, int(int, char)>::Apply apply = &T<int, char>::f;
}
Pretty ugly, indeed, but that's what the OP (maybe) asked.
I know I can do this:
template<typename T, typename Ret, typename A1, typename A2, Ret(T::*F)(A1, A2)>
class C{}
But as you can see this A1 and A2 are bit ugly. In fact I don't know the number of arguments. Sounds like a work for variadic templates. Unfortunately I can't do this:
// doesn't work - parameter pack must appear at the end of the template parameter list
template<typename T, typename Ret, typename... Args, Ret(T::*F)(Args...)>
class C{}
Nor this:
template
class C;
// doesn't work - wrong syntax
template<typename T, typename F, typename Ret, typename... Args>
class Delegate2<Ret(T::*F)(Args...)>{}
Do I want too much?
You could do the following:
template<typename T, T> struct C;
template<typename T, typename R, typename ...Args, R (T::*F)(Args...)>
struct C<R (T::*)(Args...), F> {
R operator()(T &obj, Args &&... args) {
return (obj.*F)(std::forward<Args>(args)...);
}
};
and then in your program:
struct A {
int foo(int i) { return i; }
};
int main() {
C<int(A::*)(int), &A::foo> c;
A a;
std::cout << c(a, 42) << std::endl;
}
Live Demo
template<class T>struct tag{using type=T;};
template<class Tag>using type=typename Tag::type;
template<class T, class Sig>
struct member_function_pointer;
template<class T, class Sig>
using member_function_pointer_t=type<member_function_pointer<T,Sig>>;
template<class T, class R, class...Args>
struct member_function_pointer<T, R(Args...)>:
tag<R(T::*)(Args...)>
{};
then
template<class T, class Sig, member_function_pointer_t<T,Sig> mf>
class C{};
should do the trick. If you need access to Args..., you can specialize.
template<class T, class Sig, member_function_pointer_t<T,Sig> mf>
class C;
template<class T, class R, class...Args, member_function_pointer_t<T,R(Args...)> mf>
class C<T, R(Args...), mf> {
};
like that.