Re: how to make a common template class that wraps alternate template
classes without having to write all of the interfaces?
On 7/24/2012 3:33 AM, Juha Nieminen wrote:
tonyk <tkirke@gmail.com> wrote:
Hi,
I'm wondering what is the most elegant way to do the following
Assume there are 2 or more different vendor fixed-point template classes
such as
template <int T> v1_int<T>; // always signed
and
template <int T, bool S> v2_int<T,S>; // S for signed/unsigned
(not different template params)
Now I'd like to create a template class
template <int T> V_int<T>;
such that with a #define or another technique have V_int represent either
v1_int<T> or v2_int<T,true> without having to re-write all of the member
interfaces, overloads, etc
is that possible? Any advice appreciated
If you are using C++11, you can use a template alias for that exact
purpose. It would be something like this:
#ifdef SOMETHING
template<int T> using V_int = v1_int<T>;
#else
template<int T> using V_int = v2_int<T, true>;
#endif
The above doesn't work in C++98, and I'm not sure now what would be the
easiest alternative (if there is one in the first place).
There was/is no direct alternative to template aliases. One could wrap
the other template definition, but it will require another level of
indirection:
#ifdef SOMETHING
template<int T> struct V_int { typedef v1_int<T> type; }
#else
template<int T> struct V_int { typedef v2_int<T,true> type; }
#endif
and use V_int<T>::type instead. The "::type" is the indirection.
V
--
I do not respond to top-posted replies, please don't ask