On Tuesday, July 24, 2012 5:16:14 AM UTC-7, Victor Bazarov wrote:
> On 7/24/2012 3:33 AM, Juha Nieminen wrote:
> > tonyk <> 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
Thanks, I will try this. Indeed this needs to work for g++ 4.2.x and i can't use C++11.
Tony
On Tuesday, July 24, 2012 5:16:14 AM UTC-7, Victor Bazarov wrote:
> On 7/24/2012 3:33 AM, Juha Nieminen wrote:
> > tonyk <> 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
On Tuesday, July 24, 2012 5:16:14 AM UTC-7, Victor Bazarov wrote:
> On 7/24/2012 3:33 AM, Juha Nieminen wrote:
> > tonyk <> 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