How to use "using" keyword for variadic template - c++

I have a variadic template class that can take any number of variables in the constructor and also a std::tuple/std::pair and so forth.
I would like to use this wrapper for functions with varying return types.
For example:
class f1
{
using output = double;
output operator() { do_smth };
}
class f2
{
using output = std::tuple<double,int>;
output operator() { do_smth };
}
template <typename... Types>
class OutputType
{
std::tuple<Types...> m_val;
public:
OutputType(std::tuple<Types...>&& val) : m_val(val) {};
OutputType(Types&& ... val) : m_val(std::forward<Types>(Types)...) {};
};
Now in a third class I would like to declare using like this:
template <typename F>
class dummy
{
using Output = typename OutputType(typename F::Output));
}
How do I declare the above using statement so it does the correct thing also for dummy<f2>?
(i.e OutputType<double,int> and not OutputType<std::tuple<double,int>>)

If I understand correctly your question (?), you can define a type-traits as
template <typename ... Types>
struct oTypes
{ using type = OutputType<Types...>; };
template <typename ... Types>
struct oTypes<std::tuple<Types...>>
{ using type = OutputType<Types...>; };
and then define dummy as follows
template <typename F>
struct dummy
{ using output = typename oTypes<typename F::output>::type; };
The following is a full compilable example
#include <tuple>
#include <utility>
struct f1
{
using output = double;
output operator() ()
{ return 0.0; }
};
struct f2
{
using output = std::tuple<double,int>;
output operator() ()
{ return { 1.0, 2 }; }
};
template <typename ... Types>
class OutputType
{
private:
std::tuple<Types...> m_val;
public:
OutputType(std::tuple<Types...>&& val) : m_val(val)
{ }
OutputType(Types&& ... val) : m_val(std::forward<Types>(val)...)
{ }
};
template <typename ... Types>
struct oTypes
{ using type = OutputType<Types...>; };
template <typename ... Types>
struct oTypes<std::tuple<Types...>>
{ using type = OutputType<Types...>; };
template <typename F>
struct dummy
{ using output = typename oTypes<typename F::output>::type; };
int main()
{
static_assert( std::is_same<dummy<f1>::output,
OutputType<double>>::value, "!");
static_assert( std::is_same<dummy<f2>::output,
OutputType<double, int>>::value, "!!");
}

A helper template like
template<typename ... Types>
struct add_tuple {
using type = std::tuple<Types...>
};
template<typename ... Types>
struct add_tuple<std::tuple<Types...>> {
using type = std::tuple<Types...>
};
Which changes OutputType to something like
template <typename... Types>
class OutputType
{
typename add_tuple<Types...>::type m_val;
public:
OutputType(typename add_tuple<Types...>::type&& val) : m_val(val) {};
OutputType(Types&& ... val) : m_val(std::forward<Types>(Types)...) {};
};

Related

Call template-function with types in a tuple (not values)

I would like to call a template function with no arguments for each type in a tuple. The code below shows exactly what the intention is.
My solution involves making a dummy instance of DataGroup(). I'd like to avoid this, as the types may not have a default constructor.
I've attempted to use std::declval<DataGroup>() instead, this results in
'std::declval': Symbol involving type with internal linkage not defined (in msvc).
#pragma once
#include <tuple>
template<typename T>
void do_something_based_on_the_type()
{
// ...
}
template<template <typename...> typename Tuple, typename... Ts>
void do_something_based_on_the_types_in_a_tuple(Tuple<Ts...>)
{
(do_something_based_on_the_type<Ts>(), ...);
}
void some_code()
{
struct Dataset1 {};
struct Dataset2 {};
struct Dataset3 {};
using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
do_something_based_on_the_types_in_a_tuple(DataGroup()); // -> ugly? requires a dummy instantiation of the tuple
}
My preferred solution, based on two of the answers together:
namespace internal
{
template<template <typename...> typename Tuple, typename TemplateFunc, typename... Ts>
void call_per_type_in_tuple(std::type_identity<Tuple<Ts...>>, TemplateFunc f)
{
(f.template operator () < Ts > (), ...);
}
}
template<typename Tuple, typename TemplateFunc>
void call_foreach_tuple_type(TemplateFunc f)
{
internal::call_per_type_in_tuple(std::type_identity<Tuple>(), std::forward<TemplateFunc>(f));
}
void example()
{
struct Dataset1 {};
struct Dataset2 {};
struct Dataset3 {};
using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
call_foreach_tuple_type<DataGroup>([]<typename T>()
{
// ...
});
}
You can pass in std::type_identity which is always default-constructible
#include <type_traits>
template<template <typename...> typename Tuple, typename... Ts>
void do_something_based_on_the_types_in_a_tuple(std::type_identity<Tuple<Ts...>>)
{
(do_something_based_on_the_type<Ts>(), ...);
}
using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
do_something_based_on_the_types_in_a_tuple(std::type_identity<DataGroup>());
Variant with class template specialization:
#include <tuple>
template<typename T>
void do_something_based_on_the_type()
{
// ...
}
template<typename x_Tuple>
class t_ForEachItem;
template<typename... x_Items>
class t_ForEachItem<::std::tuple<x_Items...>>
{
public: static void
Do(){ (do_something_based_on_the_type<x_Items>(), ...); }
};
void some_code()
{
struct Dataset1 {};
struct Dataset2 {};
struct Dataset3 {};
using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
t_ForEachItem<DataGroup>::Do();
}
It can also be used with a function call:
template<typename x_Tuple>
void do_something_based_on_the_types_in_a_tuple()
{
t_ForEachItem<x_Tuple>::Do();
}
...
do_something_based_on_the_types_in_a_tuple<DataGroup>();
Generic variant:
#include <tuple>
template<typename x_Tuple>
class t_ForEachItem;
template<typename... x_Items>
class t_ForEachItem<::std::tuple<x_Items...>>
{
public: template<typename x_Fun> static void
Do(x_Fun fun){ (fun.template operator ()<x_Items>(), ...); }
};
void some_code()
{
struct Dataset1 {};
struct Dataset2 {};
struct Dataset3 {};
using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
t_ForEachItem<DataGroup>::Do([]<typename T>(){});
}
online compiler
My solution is to use a helper function for type deduction. This helper function will take a pointer to a DataGroup instance, and use it to deduce the types in the tuple.
template<typename T>
void do_something_based_on_the_type()
{
// ...
}
template<typename T>
void do_something_based_on_the_types_in_a_tuple_helper(T* dataGroup)
{
std::apply([](auto&&... args){ (do_something_based_on_the_type<decltype(args)>(), ...); }, *dataGroup);
}
void some_code()
{
struct Dataset1 {};
struct Dataset2 {};
struct Dataset3 {};
using DataGroup = std::tuple<Dataset1, Dataset2, Dataset3>;
DataGroup dataGroup;
do_something_based_on_the_types_in_a_tuple_helper(&dataGroup);
}

C++ conditional template member function

I am trying to understand how to use std::enable_if to choose between 2 functions implementation. In this case, if the type TupleOfCallback doesn't contains all the type, it will not compile because std::get<...> will throw an error.
For exemple:
Executor<Entity1*, Entity2*> task([](Entity1 *e){}, [](Entity2 *2){});
This will not compile because Entity3* is not part of the tuple.
It seem that we can choose between two functions with the same prototype,
void Exec(Entity3 *entity)
{
//enabled when Entity3* is **not** in the tuple
}
OR
void Exec(Entity3 *entity)
{
//enabled when Entity3 is in the tuple
std::get<std::function<void(Entity3*)>>(m_Callbacks)(entity);
}
But i dont understand how to achieve this goal.
C++ template mechanism is still hard for me, any help is welcome.
template<typename ...T>
class Executor
{
typedef std::tuple<std::function<void(T)>...> TupleOfCallback;
public:
Executor(const std::function<void(T)> &...func)
{
}
void Exec(Entity1 *entity)
{
std::get<std::function<void(Entity1*)>>(m_Callbacks)(entity);
}
void Exec(Entity2 *entity)
{
std::get<std::function<void(Entity2*)>>(m_Callbacks)(entity);
}
void Exec(Entity3 *entity)
{
std::get<std::function<void(Entity3*)>>(m_Callbacks)(entity);
}
public:
TupleOfCallback m_Callbacks;
};
Building on this one Check if parameter pack contains a type. You can use two traits to select which method to call:
#include <iostream>
#include <type_traits>
struct Entity1 {};
struct Entity2 {};
struct Entity3 {};
template<typename What, typename ... Args>
struct is_present {
static constexpr bool value {(std::is_same_v<What, Args> || ...)};
};
template<typename T>
struct is_entity : is_present<T,Entity1,Entity2,Entity3> {};
template <typename T, typename ...Args>
struct is_present_entity {
static constexpr bool value = is_present<T,Args...>::value && is_entity<T>::value;
};
template <typename T, typename ...Args>
struct is_not_present_entity {
static constexpr bool value = (!is_present<T,Args...>::value) && is_entity<T>::value;
};
template<typename ...T>
class Executor
{
public:
template <typename U, std::enable_if_t< is_present_entity<U,T...>::value,bool> = true>
void Exec(U* t){
std::cout << "foo\n";
}
template <typename U, std::enable_if_t< is_not_present_entity<U,T...>::value,bool> = true>
void Exec(U* t){
std::cout << "bar\n";
}
};
struct foo {};
int main(void) {
Executor<Entity1,Entity2> ex;
Entity1 e1;
ex.Exec(&e1);
Entity3 e3;
ex.Exec(&e3);
// foo f;
// ex.Exec(&f);
}
output:
foo
bar
Another C++17 option:
template <typename T>
class ExecutorLeaf
{
public:
std::function<void(T)> callback;
void Exec(T entity) { callback(entity); }
};
template <typename... Ts>
class Executor : ExecutorLeaf<Ts>...
{
public:
Executor(const std::function<void(Ts)>&...funcs) : ExecutorLeaf<Ts>{funcs}... {}
using ExecutorLeaf<Ts>::Exec...; // C++17
// Fallback
template <typename T> void Exec(T) {}
};
Demo
If you can guarantee that all types appear only once, then the following should work:
template<typename... Ts>
class Executor {
using TupleOfCallback = std::tuple<std::function<void(Ts)>...>;
public:
Executor(const std::function<void(Ts)>&... func);
template<class E>
std::enable_if_t<(std::is_same_v<Ts, E*> || ...)>
Exec(E* entity) {
std::get<std::function<void(E*)>>(m_Callbacks)(entity);
}
template<class E>
std::enable_if_t<!(std::is_same_v<Ts, E*> || ...)>
Exec(E* entity)
{ }
public:
TupleOfCallback m_Callbacks;
};
The basic idea is to use fold-expression to detect whether E* is included in Ts..., thereby enabling the corresponding function.
Demo.

ignore template parameter in type matching

This is my current program:
#include <type_traits>
template<class Feature, class... FeatureList>
struct has_feature {
static constexpr bool value = (std::is_same_v<Feature, FeatureList> || ...);
};
template<class Feature, class... FeatureList>
inline constexpr bool has_feature_v = has_feature<Feature, FeatureList...>::value;
template<class Feature, class ...FeatureList>
static constexpr bool isConfiguredWith() {
return has_feature_v<Feature, FeatureList...>;
}
struct CanWalk {
};
struct CanNotWalk {
};
template<class... FeatureList>
struct Robot {
static auto configure() {
return Robot<WalkFeature < FeatureList...>>
();
}
private:
template<typename ...Config>
using WalkFeature =
std::conditional_t<isConfiguredWith<CanWalk, Config...>(), CanWalk, CanNotWalk>;
};
int main() {
Robot<CanWalk> robot_A = Robot<CanWalk>::configure();
Robot<CanNotWalk> robot_B = Robot<>::configure();
return 0;
}
Basically, Robot is a struct that can be configured with many other struct (they are used as token here), then Robot<T...>::configure() trim and organize the template parameters passed into to Robot. In the end we have:
Robot<CanWalk> robot_A = Robot<CanWalk, CanWalk>::configure();
Robot<CanNotWalk> robot_B = Robot<>::configure();
Although duplicated features CanWalk are passed into as template parameters, they are all deleted when constructing Robot via function configure().
This is working well until I add a template parameter to feature CanWalk:
template <int Speed>
struct CanWalk {
};
Now everything breaks since CanWalk is no longer a legit type, it needs a template parameter.
For error error: use of class template 'CanWalk' requires template arguments occured from:
template<typename ...Config>
using WalkFeature =
std::conditional_t<isConfiguredWith<CanWalk>(), CanWalk, CanNotWalk>;
How do I fix it?
How can I define them as:
Robot<CanWalk<5>> robot_A = Robot<CanWalk<5>>::configure();
Robot<CanNotWalk> robot_B = Robot<>::configure();
?
live code: https://godbolt.org/z/4K9TxohWq
I see three challenges here:
you need to extract a specific trait (CanWalk), regardless of its parameters, from a parameter pack
you need to ignore extra copies of the same trait
you need to set a default trait (CanNotWalk) if that trait is not present
I don't know a better way to do this than recursively:
// definition
template<template<auto...> class FeatureType, class DefaultFeature, class... FeatureList>
struct find_feature;
// next trait matches, stop recursing and return it
template<template<auto...> class FeatureType, class DefaultFeature, auto... Param, class... RemainingFeatures>
struct find_feature<FeatureType, DefaultFeature, FeatureType<Param...>, RemainingFeatures...> {
using type = FeatureType<Param...>;
};
// next trait does not match, skip by inheriting from rest of list
template<template<auto...> class FeatureType, class DefaultFeature, class FirstFeature, class... RemainingFeatures>
struct find_feature<FeatureType, DefaultFeature, FirstFeature, RemainingFeatures...>
: find_feature<FeatureType, DefaultFeature, RemainingFeatures...> { };
// no more traits, return default trait
template<template<auto...> class FeatureType, class DefaultFeature>
struct find_feature<FeatureType, DefaultFeature> {
using type = DefaultFeature;
};
// alias
template<template<auto...> class FeatureType, class... FeatureList>
using find_feature_t = typename find_feature<FeatureType, FeatureList...>::type;
this should work for any feature that is a class template and any number of non-type parameters.
usage:
template <int speed>
struct CanWalk {};
struct CanNotWalk {};
template<class... FeatureList>
struct Robot {
static auto configure() {
return Robot<WalkFeature < FeatureList...>>
();
}
private:
template<typename ...Config>
using WalkFeature = find_feature_t<CanWalk, CanNotWalk, Config...>;
};
auto robot_A = Robot<CanWalk<42>, CanWalk<25>>::configure();
static_assert(std::is_same_v<decltype(robot_A), Robot<CanWalk<42>>>, "");
auto robot_B = Robot<>::configure();
static_assert(std::is_same_v<decltype(robot_B), Robot<CanNotWalk>>, "");
Removing some genericity, you might do something like:
template <typename T> struct Tag { using type = T; };
template <std::size_t> struct CanWalk {};
struct CanNotWalk {};
template <std::size_t N> Tag<CanWalk<N>> has_walk(Tag<CanWalk<N>>); // No impl
template <typename T> Tag<CanNotWalk> has_walk(Tag<T>); // No impl
template<class... FeatureList>
struct Robot {
static auto configure() {
struct all_features : Tag<FeatureList>..., Tag<struct Empty> {};
return Robot<typename decltype(has_walk(all_features{}))::type>{};
}
};
int main() {
[[maybe_unused]] Robot<CanWalk<42>> robot_A = Robot<CanWalk<42>>::configure();
[[maybe_unused]] Robot<CanNotWalk> robot_B = Robot<>::configure();
}
Demo
That simple way doesn't handle duplicates, but can with some extra work (idea is to have struct all_features : Tag<Ts, Is>... with std::index_sequence)

C++ template specialization - avoid redefinition

I want to have a generic function (or method) that accepts arguments of different types. If the provided type has 'one' method, the function should use it. If it has 'two' method, the function should use it instead.
Here's the invalid code:
#include <iostream>
template<typename Type> void func(Type t)
{
t.one();
}
template<typename Type> void func(Type t) // redefinition!
{
t.two();
}
class One
{
void one(void) const
{
std::cout << "one" << std::endl;
}
};
class Two
{
void two(void) const
{
std::cout << "two" << std::endl;
}
};
int main(int argc, char* argv[])
{
func(One()); // should print "one"
func(Two()); // should print "two"
return 0;
}
Is it possible to achieve using SFINAE? Is it possible to achieve using type_traits?
Clarification:
I would be more happy if this would be possible using SFINAE. The best-case scenario is: use first template, if it fails use the second one.
Checking for method existence is only an example. What I really want is also checking for compatibility with other classes.
The task could be rephrased:
If the class supports the first interface, use it.
If the first interface fails, use the second interface.
If both fail, report an error.
Yes, it's possible. In C++11 an onward it's even relatively easy.
#include <iostream>
#include <type_traits>
template<class, typename = void>
struct func_dispatch_tag :
std::integral_constant<int, 0> {};
template<class C>
struct func_dispatch_tag<C,
std::enable_if_t<std::is_same<decltype(&C::one), void (C::*)() const>::value>
> : std::integral_constant<int, 1> {};
template<class C>
struct func_dispatch_tag<C,
std::enable_if_t<std::is_same<decltype(&C::two), void (C::*)() const>::value>
> : std::integral_constant<int, 2> {};
template<class C>
void func(C const&, std::integral_constant<int, 0>) {
std::cout << "fallback!\n";
}
template<class C>
void func(C const &c, std::integral_constant<int, 1>) {
c.one();
}
template<class C>
void func(C const &c, std::integral_constant<int, 2>) {
c.two();
}
template<class C>
void func(C const &c) {
func(c, func_dispatch_tag<C>{});
}
struct One
{
void one(void) const
{
std::cout << "one\n";
}
};
struct Two
{
void two(void) const
{
std::cout << "two\n";
}
};
struct Three {};
int main(int argc, char* argv[])
{
func(One()); // should print "one"
func(Two()); // should print "two"
func(Three());
return 0;
}
Important points:
We SFINAE on the second parameter of func_dispatch_tag. The compiler looks at all the template specializations which result in the parameters <C, void>. Since any of the latter is "more specialized" when SF doesn't occur (i.e when std::enable_if_t is void), it gets chosen.
The chosen specialization of the trait defines a tag which we do a tag dispatch on. Tag dispatch depends on function overloading, instead of function template specialization (that cannot be partially specialized).
You can define a fallback function (like I did), or static_assert. The number of tags we can define is limited only by the range of an int, so extending to other members is just a matter of adding another func_dispatch_tag specialization.
The member must be accessible, or SF will occur. Also, a class that has both members will result in ambiguity. Bear that in mind.
Here's another way. There's a little more boilerplate, but in the actual expression of the different implementations of func() it could be argued that the 'list of tests that passed' is more expressive.
Food for thought anyway.
Code is c++11. c++14 and 17 would be more succinct.
#include <iostream>
#include <type_traits>
#include <tuple>
// boilerplate required prior to c++17
namespace notstd {
using namespace std;
template<typename... Ts> struct make_void { typedef void type;};
template<typename... Ts> using void_t = typename make_void<Ts...>::type;
}
// test for having member function one()
template<class T, class Enable = notstd::void_t<>> struct has_one : std::false_type {};
template<class T> struct has_one<T, notstd::void_t<decltype(std::declval<T>().one())>> : std::true_type {};
//test for having member function two()
template<class T, class Enable = notstd::void_t<>> struct has_two : std::false_type {};
template<class T> struct has_two<T, notstd::void_t<decltype(std::declval<T>().two())>> : std::true_type {};
// a type collection of tests that pass
template<template <class...> class...Tests> struct passes_tests {
};
// meta-function to append a type
template<class Existing, template <class...> class Additional> struct append_pass;
template< template <class...> class...Tests, template <class...> class Additional>
struct append_pass<passes_tests<Tests...>, Additional> {
using type = passes_tests<Tests..., Additional>;
};
//
// meta-functions to compute a list of types of test that pass
//
namespace detail
{
template<class Previous, class T, template<class...> class Test, template<class...> class...Rest>
struct which_tests_pass_impl
{
using on_pass = typename append_pass<Previous, Test>::type;
using on_fail = Previous;
using this_term = typename std::conditional< Test<T>::value, on_pass, on_fail >::type;
using type = typename which_tests_pass_impl<this_term, T, Rest...>::type;
};
template<class Previous, class T, template<class...> class Test>
struct which_tests_pass_impl<Previous, T, Test>
{
using on_pass = typename append_pass<Previous, Test>::type;
using on_fail = Previous;
using this_term = typename std::conditional< Test<T>::value, on_pass, on_fail >::type;
using type = this_term;
};
}
template<class Type, template<class...> class...Tests> struct which_tests_pass
{
using type = typename detail::which_tests_pass_impl<passes_tests<>, Type, Tests...>::type;
};
//
// various implementations of func()
//
namespace detail
{
template<class T>
void func(T t, passes_tests<has_one>)
{
t.one();
}
template<class T>
void func(T t, passes_tests<has_one, has_two>)
{
t.one();
}
template<class T>
void func(T t, passes_tests<has_two>)
{
t.two();
}
template<class T>
void func(T t, passes_tests<>)
{
// do nothing
}
}
template<class T>
void func(T t)
{
detail::func(t, typename which_tests_pass<T, has_one, has_two>::type());
}
//
// some types
//
struct One
{
void one(void) const
{
std::cout << "one" << std::endl;
}
};
struct Two
{
void two(void) const
{
std::cout << "two" << std::endl;
}
};
// test
int main(int argc, char* argv[])
{
func(One()); // should print "one"
func(Two()); // should print "two"
return 0;
}
The code below
handles member function constness correctly
is agnostic of the functions return types
prints a comprehensive error on failure
It could be even shorter with C++14, where you don't have to specify the return types of implemented functions and have templated variable declarations. If you want to handle rvalue overloads correctly, you need to provide another overload to as_memfun.
If testing for member functions alone is not enough, there is another approach in the last section, which offers far better customization options but is also lengthier to setup.
#include <utility>
#include <functional>
namespace detail {
template<typename T> struct _false : std::integral_constant<bool, false> { };
template<typename T> struct HasNone {
static_assert(_false<T>::value, "No valid method found");
};
template<typename T, typename R>
constexpr auto as_memfun (R (T::* arg) ())
-> R (T::*) ()
{ return arg; }
template<typename T, typename R>
constexpr auto as_memfun (R (T::* arg) () const)
-> R (T::*) () const
{ return arg; }
template<typename T> constexpr auto check_has_two(int)
-> decltype(as_memfun(&T::two))
{ return as_memfun(&T::two); }
template<typename T> constexpr auto check_has_two(...)
-> HasNone<T>;
template<typename T> constexpr auto check_has_one(int)
-> decltype(as_memfun(&T::one))
{ return as_memfun(&T::one); }
template<typename T> constexpr auto check_has_one(...)
-> decltype(check_has_two<T>(0))
{ return check_has_two<T>(0); }
template<typename T>
struct res { constexpr static auto detail = check_has_one<T>(0); };
}
template<typename T>
auto func(T t) -> decltype((t.*detail::res<T>::detail)()) {
return (t.*detail::res<T>::detail)();
}
And here are some test you would probably like to have
struct One {
void one();
};
struct Two {
void two();
};
struct TestBoth {
char one() const;
void two();
};
struct TestWilderStuff {
int one;
void two() const;
};
int main() {
func(One{});
func(Two{});
func(TestBoth{});
static_assert(decltype(func(TestBoth{})){} == 0, "Failed function selection");
func(TestWilderStuff{});
}
Since you seem to have more extensive constructions in mind than just testing for member function existence, here is the beginning of a vastly more powerful mechanism. You can use it as a drop-in replacement for the above solution and although it is far lengthier, it offers more customization and the possibility to do elaborate tests on your types in every step of the way.
#include <utility>
#include <functional>
namespace detail {
template<typename T> struct _false :
std::integral_constant<bool, false> { };
template<typename T> struct HasNone {
static_assert(_false<T>::value, "No valid method found");
};
// Generic meta templates used below
namespace Generics {
template<typename Getter, typename Else>
struct ChainGetter {
template<typename T> constexpr static auto get_(int)
-> decltype(Getter::template get<T>())
{ return Getter::template get<T>(); }
template<typename T> constexpr static auto get_(...)
-> decltype(Else::template get<T>())
{ return Else::template get<T>(); }
template<typename T> constexpr static auto get()
-> decltype(get_<T>(0))
{ return get_<T>(0); }
};
template<typename Getter, typename Test>
struct TestGetter {
template<typename T, typename R> using _type = R;
template<typename T> constexpr static auto get_()
-> decltype(Getter::template get<T>())
{ return Getter::template get<T>(); }
template<typename T> constexpr static auto test()
-> decltype(Test::template test<T>(get_<T>()));
template<typename T> constexpr static auto get()
-> _type<decltype(test<T>()),
decltype(get_<T>())
>
{ return get_<T>(); }
};
template<template<typename> class F>
struct FailGetter {
template<typename T>
constexpr static auto get() -> F<T>;
};
}
// Test only exists for member function pointer arguments
struct IsMemberFunctionTest {
template<typename _, typename T, typename R>
constexpr static void test (R (T::* arg) ());
template<typename _, typename T, typename R>
constexpr static void test (R (T::* arg) () const);
};
// Get member pointer to T::one
struct GetOne {
template<typename T>
constexpr static auto get() -> decltype(&T::one) { return &T::one; }
};
// Get member pointer to T::two
struct GetTwo {
template<typename T>
constexpr static auto get() -> decltype(&T::two) { return &T::two; }
};
using namespace Generics;
using getter_fail = FailGetter<HasNone>;
using get_two_tested = TestGetter<GetTwo, IsMemberFunctionTest>;
using getter_two = ChainGetter<get_two_tested, getter_fail>;
using get_one_tested = TestGetter<GetOne, IsMemberFunctionTest>;
using getter_one = ChainGetter<get_one_tested, getter_two>;
template<typename T>
struct result { constexpr static auto value = getter_one::template get<T>(); };
}
template<typename T>
auto func(T t) -> decltype((t.*detail::result<T>::value)()) {
return (t.*detail::result<T>::value)();
}

C++ Mixins of 2 functional types

I'm trying to write code that is effectively:
template <typename... Mixins>
class Checker : public Mixins... {
public:
template<typename... Args>
Checker(Args&&... args) : Mixins(std::forward<Args>(args))... { }
bool check(void) {
bool tests = true;
// TODO: for each Mixins that has Mixin::check
{
tests = tests && Mixin::check();
}
if (!tests) {
// TODO: for each Mixin that has Mixin::handle
{
Mixin::handle()
}
}
return tests
}
};
I just don't know how to do the loops over just those Mixins satisfying the member conditions specified. I've tried various solutions for iteration and for enable_if type semantics but nothing seems to compile.
Additional
So, what I've tried so far visa vi for-each base recursion and SFINAE function elimination is the following, which doesn't compile:
template <typename C, typename... Mx>
struct do_checks;
template <typename C>
struct do_checks<C> {
static bool check(C *) {
return true;
}
};
template <typename C, typename E, typename... Mx>
struct do_checks<C, E, Mx...> {
struct general_ { };
struct special_ : general_ { };
template <typename> struct int_ { typedef int type; };
template <typename F, typename int_<decltype(F::check)>::type = 0>
static bool check(C *ptr, special_) {
if (!ptr->F::check()) {
return false;
}
return do_checks<C, Mx...>::check(ptr);
}
template <typename F>
static bool check(C *ptr, general_) {
return do_checks<C, Mx...>::check(ptr);
}
};
Where F should be E. I don't know how to tell the compiler to compile the first "check" if the int_ has a type and the second if it doesn't.
You can create a trait named has_check which returns true for a type which has a static check() method.
namespace detail
{
template<class T, class = decltype(T::check())>
std::true_type has_check_impl(void*);
template<class T>
std::false_type has_check_impl(...);
}
template<class T>
struct has_check : decltype(detail::has_check_impl<T>(nullptr)) { };
Then you can use it as the predicate to std::enable_if to enable/disable the overload if it has the method:
private:
template<class T, class...>
using first = T;
template<class T = void,
class =
first<T,
typename std::enable_if<has_check<Mixins>::value, T>::type...>>
static void foo(void*)
{
auto x{ (0, Mixins::check(), 0)... };
(void)x;
}
static void foo(...)
{
auto x{ (0, Mixins::handle(), 0)... };
(void)x;
}
public:
static void check() {
foo(nullptr);
}