I am working on a library that has several template classes, all of them are so interconnected that they all have the same template settings. In a sense, this set of template parameters describes an almost self-contained class universe, possibly with one or two routes between these class universes.
In current C ++, I donβt have a real way to group such "class universes" together, except for the rather ugly and manual way. Here is an example of some interconnected patterns that can highlight my point:
template<size_t dimension>
using vector_t = std::array<double, dimension>;
template<typename weight_t, size_t outer_dimension, size_t inner_dimension>
class A{
protected:
weight_t weight;
public:
weight_t get_weight(
const vector_t<outer_dimension>& foo,
const vector_t<inner_dimension>& bar
) const;
};
template<typename weight_t, size_t outer_dimension, size_t inner_dimension>
class B{
protected:
std::vector<std::shared_ptr<A<weight_t, outer_dimension, inner_dimension>>> A_vector;
};
template<typename weight_t, size_t outer_dimension, size_t inner_dimension>
class C{
protected:
std::vector<std::shared_ptr<A<weight_t, outer_dimension, inner_dimension>>> A_vector;
std::vector<std::shared_ptr<B<weight_t, outer_dimension, inner_dimension>>> B_vector;
public:
typename std::enable_if<
(inner_dimension > 0),
C<weight_t, outer_dimension + 1, inner_dimension - 1>
>::type do_something() const;
};
This is useful - and in the real library I'm working on, the structure is perfect for what I want to use it for. This, on the other hand, is extremely ugly and quite frank for me.
++, - :
template<size_t dimension>
using vector_t = std::array<double, dimension>;
template<typename weight_t, size_t outer_dimension, size_t inner_dimension>
namespace universe{
typedef vector_t<outer_dimension> outer_vector;
typedef vector_t<inner_dimension> inner_vector;
class A{
protected:
weight_t weight;
public:
weight_t get_weight(const outer_vector& foo, const inner_vector& bar) const;
};
class B{
protected:
std::vector<std::shared_ptr<A>> A_vector;
};
class C{
protected:
std::vector<std::shared_ptr<A>> A_vector;
std::vector<std::shared_ptr<B>> B_vector;
public:
typename std::enable_if<
(inner_dimension > 0),
universe<weight_t, outer_dimension + 1, inner_dimension - 1>::C
>::type do_something() const;
};
}
, ++. , , . "templating" . :
- typedefs,
- typedefs ,
templating .
- , , , (IMO).
, - . , , , ( )?
- , , typedef, .