C++03 allows to create template class which inheritances from template parameters:
// c++03
struct NullType {};
template <class T0, class T1 = NullType, class T2 = NullType>
class Collector : public T0, public T1, public T2
{
};
template <class T0, class T1>
class Collector<T0, T1, NullType> : public T0, public T1
{
};
template <class T0>
class Collector<T0, NullType, NullType> : public T0
{
};
So
typedef Collector<A, B, C> X;
eqeals to
class X: public A, public B, public C {};
C++11 allows do it easier:
// variadic templates - great thing!
template <class ... Classes>
class C11_Collector :
public Classes ...
{
};
Wrapping collector must wraps template parameters before inheritence:
template <template <class> class Wrap, class T0, class T1 = NullType, class T2 = NullType>
class Wrapping_Collector : public Wrap<T0>, public Wrap<T1>, public Wrap<T2>
{
};
template <template <class> class Wrap, class T0, class T1>
class Wrapping_Collector<Wrap, T0, T1, NullType> : public Wrap<T0>, public Wrap<T1>
{
};
template <template <class> class Wrap, class T0>
class Wrapping_Collector<Wrap, T0, NullType, NullType> : public Wrap<T0>
{
};
So
typedef Wrapping_Collector<W, A, B> X;
eqeals to
class X: public W<A>, public W<B> {};
How implement Wrapping_Collector in more easer way by c++11?
Is it possible to just enforce Wrap<NullType> to be an empty class? Then you could just directly use
template <template <typename> class Wrap, typename... Types>
class Wrapping_Collector : public Wrap<Types>... {
//...
};
Alternatively, if a double-inheritance chain instead of direct multi-inheritance is fine, you could make Wrapping_Collector<Wrap, A, B...> derive from both Wrapping_Collector<Wrap, B...> and Wrap<A>:
template <template <typename> class Wrap, typename... Types>
class Wrapping_Collector;
// The normal case
template <template <typename> class Wrap, typename Head, typename... Rest>
class Wrapping_Collector<Wrap, Head, Rest...>
: public Wrapping_Collector<Wrap, Rest...>, Wrap<Head>
{
//...
};
// Ignore on NullType
template <template <typename> class Wrap, typename... Rest>
class Wrapping_Collector<Wrap, NullType, Rest...>
: public Wrapping_Collector<Wrap, Rest...>
{
//...
};
// Base case
template <template <typename> class Wrap>
class Wrapping_Collector<Wrap> {};
I believe you would do this in C++11:
// variadic templates - great thing!
template <template <class> class Wrap, class... Classes>
class C11_Wrapping_Collector : public Wrap<Classes>...
{
};
Related
I have many derived classes from base. Those classes must inherit constructor from base, but that constructor should only work with derived or base class instances.
Base class example:
template<typename T, typename U>
struct bar
{
bar() = default;
template<typename _Bar_or_Derived>
bar(const _Bar_or_Derived &); // must accept any bar or its derived classes
};
Derived classes example:
template<typename T, typename U>
struct foo : public bar<T, U>
{
using bar<T, U>::bar;
// must inherit something like foo(const Foo_or_Bar&)
};
template<typename T, typename U>
struct not_foo : public bar<T, U>
{
using bar<T, U>::bar;
// must inherit something like not_foo(const NotFoo_or_Bar&)
};
How to do such a thing?
It seems you want CRTP instead of common base class to avoid to duplicate code:
template <typename > struct Bar;
template <template <typename, typename> class C, typename T1, typename T2>
struct Bar<C<T1, T2>>
{
Bar(const Bar&) {/*..*/}
template <typename U1, U2>
Bar(const Bar<C<U1, U2>>&) {/*..*/}
template <typename U1, U2>
Bar(const C<U1, U2>&) {/*..*/}
};
// Maybe you just need template <template <typename, typename> class C> struct Bar{};
// instead, as T1, T2 seems not used
template<typename T, typename U>
struct foo : public bar<foo>
{
using bar<foo>::bar;
};
template<typename T, typename U>
struct not_foo : public bar<not_foo>
{
using bar<not_foo>::bar;
};
I have a template class receiving multiple types, each type received is subclass of one of two options.
I want to expand them differently depending the parent class identifying each of them. This is equivalent to implement "filter" over the variadic template parameters.
For example:
class A{};
class B{};
template<class... C>
struct F{
std::tuple<types_derived_by<A, C>...> fn(types_subclassing<B, C>...){}
};
The types_derived_by template function should produce a variadic template pack with all the types in C that are derived from A, or B.
For example:
struct DA : public A{};
struct DB : public B{};
int main(){
F<DA, DB> f;
//f has a member function: DA fn(DB);
}
I'm using C++11, but I'm ok to move to c++14 if necessary.
You may do something like:
template <template <typename> class Pred, typename TUPLE, typename Res = std::tuple<>>
struct Filter;
template <template <typename> class Pred, typename Res>
struct Filter<Pred, std::tuple<>, Res>
{
using type = Res;
};
template <template <typename> class Pred, typename T, typename ... Ts, typename ... TRes>
struct Filter<Pred, std::tuple<T, Ts...>, std::tuple<TRes...>> :
Filter<Pred,
std::tuple<Ts...>,
std::conditional_t<Pred<T>::value,
std::tuple<TRes..., T>,
std::tuple<TRes...>>>
{
};
and then:
class A {};
template <typename T>
using is_base_of_A = std::is_base_of<A, T>;
class B {};
struct DA : public A{};
struct DB : public B{};
struct DA1 : public A{};
static_assert(std::is_same<std::tuple<DA, DA1>,
Filter<is_base_of_A, std::tuple<DA, DB, DA1>>::type>::value,
"unexpected");
Demo
If you don't mind using tuples as return value and parameter this might be a solution for you:
template <typename Base, typename...T>
struct base_filter;
template <typename Base>
struct base_filter<Base>
{
using type = std::tuple<>;
};
template <typename Base, typename T1>
struct base_filter<Base, T1>
{
using type = typename std::conditional_t<std::is_base_of<Base, T1>::value, std::tuple<T1>, std::tuple<>>;
};
template <typename Base, typename T1, typename...T>
struct base_filter<Base, T1, T...>
{
using type = decltype(std::tuple_cat(base_filter<Base, T1>::type(), base_filter<Base, T...>::type()));
};
//###########################################################
class A {};
class B {};
template<class...C>
struct F {
typename base_filter<A, C...>::type fn(typename base_filter<B, C...>::type){}
};
struct DA : public A {};
struct DB : public B {};
struct DA1 : public A {};
struct DA2 : public A {};
struct DB1 : public B {};
struct DB2 : public B {};
int main() {
std::tuple<DB> b;
F<DA, DB> f1;
std::tuple<DA> a = f1.fn(b);
std::tuple<DB1, DB2> bb;
F<DB1, DA1, DB2, DA2> f2;
std::tuple<DA1, DA2> aa = f2.fn(bb);
}
I'm trying to assert that template parameter would be derived from some base class. But base class is generic and in the context of assertion there is no difference between any specialization types.
How can I assert, that template parameter were derived from generic of any specialized type?
I'm trying to write it as
base_generic:
template<typename T> struct base_generic{};
derived_generic:
template<typename T> struct derived_generic : public base_generic<T>{};
class with assertion:
template<typename Tsource, typename Tderived_generic>
struct encoder {
static_assert(std::is_base_of<base_generic<typename>, Tderived_generic>::value);
};
This code compiles, but assertion fails
You may create a trait for that, something like:
namespace detail
{
template <template <typename > class C>
struct is_base_of_any_helper
{
template <typename T>
std::true_type operator ()(const C<T>*) const;
std::false_type operator() (...) const;
};
}
template <template <typename > class C , typename T>
using is_base_of_any =
decltype(detail::is_base_of_any_helper<C>{}(std::declval<const T*>()));
Demo
Note that it will fail with some edge cases such as:
multiple bases C<Tx>
private inheritance of C<T>.
From commentary of #PiotrSkotnicki
template <template <typename...> class Base, typename Derived>
struct is_base_of_template
{
using U = typename std::remove_cv<Derived>::type;
template <typename... Args>
static std::true_type test(Base<Args...>*);
static std::false_type test(void*);
using type = decltype(test(std::declval<U*>()));
};
template <template <typename...> class Base, typename Derived>
using is_base_of_template_t = typename is_base_of_template<Base, Derived>::type;
This solution works fine, example.
you can use static_assert.
See: http://en.cppreference.com/w/cpp/language/static_assert
So for example, if you have a base class
template< typename T >
class Base<T> {};
and the derived one
template< typename T >
class Derived : public Base<T> {};
with your use case
template< typename Tsource, typename Tstorage >
class Test
{
std::static_assert( std::is_base_of< base<T>, TStorage >::value );
}
It is not possible to get T without adding it as a template parameter.
But with a simple trick, you get it. You have to add a typedef to Derived:
template< typename T >
class Derived : public Base<T>
{
public:
typedef T value_type;
};
And you can use it inside of the static assertion.
template< typename Tsource, typename Tstorage >
class Test
{
std::static_assert( std::is_base_of< base<TStorage::value_type>, TStorage >::value );
}
Since Tstorage is a Derived, it has the value_type field.
Is it possible to generate a linear hierarchy from variadic template parameters? For example:
GenLinearHierarchy<A,B,C,D,...> linHierarchy;
Generates a hierarchy where A -> B -> C -> D -> ... -> Empty (where the -> symbol stands for inherits from).
Where the template parameters (template template... parameters) have signatures such as:
template <class Base, class Plate> class A;
Where Base is the class above A in the hierarchy and Plate is the 'most derived' class (for example D< E<...>, A<...> >).
So far I've been unsuccessful - starting to wonder whether I'm going in circles (I do keep running into cyclic problems).
Sorry about the confusion - here is a 'concrete solution' (one which I'm not too fond of):
// Linear Hierarchy
#include <iostream>
template <class Base, class Floor>
class D : public Base
{
public:
};
template <class Base, class Floor>
class C : public Base
{
public:
void Fire()
{
static_cast<Floor*>(this)->Handle();
}
};
template <class Base, class Floor>
class B : public Base
{
public:
void Handle()
{
std::cout << "Handled" << std::endl;
}
};
class _FINISH {};
class _START : public B<C<D<_FINISH, _START>, _START, _START> {};
int main()
{
typedef _START A;
A a;
a.Fire();
return 0;
}
So, I'm still looking for a 'GenLinearHierarchy' class which can generate something like above, but with an instatiation like this:
GenLinearHierarchy<B,C,D> linHierarchy;
Thanks :)
I don't think you can let Plate be the most derived type, as that would mean the overall type would have to have itself as one of its own template parameters. This is the closest I've gotten:
struct Empty {};
template <template <class> class Head, template <class> class... Tail>
struct GenLinearHierarchy
{
typedef Head<typename GenLinearHierarchy<Tail...>::type> type;
};
template <template <class> class Tail>
struct GenLinearHierarchy<Tail>
{
typedef Tail<Empty> type;
};
template <class Base> struct A : Base {};
template <class Base> struct B : Base {};
template <class Base> struct C : Base {};
template <class Base> struct D : Base {};
static_assert(std::is_same< typename GenLinearHierarchy<A,B,C,D>::type
, A<B<C<D<Empty>>>> >::value
, "");
EDIT I think I've got what you wished for. Good luck trying to read it. You've only got yourself to blame. :)
struct Empty {};
template <class MostDerived,
template <class, class> class Head,
template <class, class> class... Tail>
struct GenLinearHierarchyInner
{
typedef Head<typename GenLinearHierarchyInner<MostDerived,
Tail...>::type,
MostDerived> type;
};
template <class MostDerived,
template <class, class> class Tail>
struct GenLinearHierarchyInner<MostDerived, Tail>
{
typedef Tail<Empty, MostDerived> type;
};
template <template <class, class> class... Types>
struct GenLinearHierarchy : GenLinearHierarchyInner<GenLinearHierarchy<Types...>,
Types...>::type
{};
template <class Base, class> struct A : Base {};
template <class Base, class> struct B : Base {};
template <class Base, class> struct C : Base {};
template <class Base, class> struct D : Base {};
typedef GenLinearHierarchy<A,B,C,D> ABCD;
static_assert(std::is_base_of< A<B<C<D<Empty, ABCD>, ABCD>, ABCD>, ABCD>
, ABCD >::value
, "");
It's probably something like this...
template<typename T, typename... Args>
class GenLinearHierarchy : public T, public GenLinearHierarchy<Args...> {};
template<typename T>
class GenLinearHierarchy<T> : public T {};
I've the following:
template <template <typename, typename> class C, class T, class A >
class TTCTest
{
public:
TTCTest(C<T, A> parameter) { /* ... some stuff... */ }
C<T, A> _collection;
};
I want to ensure that the template is only instantiated where the T and A classes are of a specific type (path and allocator respectively).
For example:
...
list<path, allocator<path> > list1;
list<int, allocator<int> > list2;
TTCTest<list> testvar(list1); // ...should work
TTCTest<list> testvar(list2); // ...should not work
...
Is this possible and what is the syntax?
Regards,
Col
You can do that with partial specialisation, where you fail to provide an implementation for the non-specialised case.
For example:
template <template <typename, typename> class C, class T, class A >
class TTCTest;
template <template <typename, typename> class C>
class TTCTest<C, path, allocator<path> >
{
// ...
};
You can create trait class to constraint instantiations. For example, limit your TTCTest construction only to path type:
template<class T, class A> class path {};
template<class T, class A> class not_path {};
template<class T> class allocation {};
template<class T>
struct Testable;
template<class T, class A>
struct Testable<path<T,A> > {};
template <template <typename, typename> class C,
class T, class A >
class TTCTest
{
public:
TTCTest(C<T, A> parameter, Testable<C<T,A> > = Testable<C<T,A> >());
C<T, A> _collection;
};
void foo()
{
path<int, allocation<int> > p;
TTCTest<path, int, allocation<int> > t(p); // compiles
not_path<int, allocation<int> > np;
TTCTest<not_path, int, allocation<int> > t1(np); // fails
}
Edit:
Since you indicated later that all you might need is partial specialization, in which case it would look like this:
template <class T, class A >
class TTCTest<path, T, A>
{
public:
TTCTest(path<T, A> parameter);
path<T, A> _collection;
};