Problem
Consider the following class
template <typename T>
struct A
{
T x;
};
Now another template is templated like this:
template <typename T, typename U>
class B
{
protected:
std::vector<U> arr;
public:
T get_x(unsigned int p) { return arr[p].x; }
};
I would like to access the field A<T>::xfrom B<T, A<T>>::get_x()and return it without changes (i.e. save its type as T). My poor knowledge of templates says that this requires knowledge of the andof type T, it should be one of the template parameters class B. However, this allows you to declare something inconsistent, for example B<int, A<double>>, and generally sounds like an unnecessary repetition.
Questions
- Did I write an example of bad programming practice? How should this be written?
- Is it possible to infer a type
Tfrom A<T>::xand avoid two types of patterns? This seems like a repetition, so I'm not sure if there is a god-fearing, standard solution or not.
, GCC 4.6.2 -std = ++ 0x.