how to make a common template class that wraps alternate templateclasses without having to write all

Discussion in 'C++' started by tonyk, Jul 23, 2012.

  1. tonyk

    tonyk Guest

    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
    tonyk, Jul 23, 2012
    #1
    1. Advertising

  2. Re: how to make a common template class that wraps alternate template classes without having to write all of the interfaces?

    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).
    Juha Nieminen, Jul 24, 2012
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Jul 24, 2012
    #3
  4. tonyk

    tonyk Guest

    On Tuesday, July 24, 2012 5:16:14 AM UTC-7, Victor Bazarov wrote:
    > On 7/24/2012 3:33 AM, Juha Nieminen wrote:
    > &gt; tonyk &lt;&gt; wrote:
    > &gt;&gt; Hi,
    > &gt;&gt; I'm wondering what is the most elegant way to do the following
    > &gt;&gt; Assume there are 2 or more different vendor fixed-point template classes
    > &gt;&gt; such as
    > &gt;&gt; template &lt;int T&gt; v1_int&lt;T&gt;; // always signed
    > &gt;&gt; and
    > &gt;&gt;
    > &gt;&gt; template &lt;int T, bool S&gt; v2_int&lt;T,S&gt;; // S for signed/unsigned
    > &gt;&gt; (not different template params)
    > &gt;&gt;
    > &gt;&gt; Now I'd like to create a template class
    > &gt;&gt; template &lt;int T&gt; V_int&lt;T&gt;;
    > &gt;&gt; such that with a #define or another technique have V_int represent either
    > &gt;&gt; v1_int&lt;T&gt; or v2_int&lt;T,true&gt; without having to re-write all of the member
    > &gt;&gt; interfaces, overloads, etc
    > &gt;&gt;
    > &gt;&gt; is that possible? Any advice appreciated
    > &gt;
    > &gt; If you are using C++11, you can use a template alias for that exact
    > &gt; purpose. It would be something like this:
    > &gt;
    > &gt; #ifdef SOMETHING
    > &gt; template&lt;int T&gt; using V_int = v1_int&lt;T&gt;;
    > &gt; #else
    > &gt; template&lt;int T&gt; using V_int = v2_int&lt;T, true&gt;;
    > &gt; #endif
    > &gt;
    > &gt; The above doesn't work in C++98, and I'm not sure now what would be the
    > &gt; 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&lt;int T&gt; struct V_int { typedef v1_int&lt;T&gt; type; }
    > #else
    > template&lt;int T&gt; struct V_int { typedef v2_int&lt;T,true&gt; type; }
    > #endif
    >
    > and use V_int&lt;T&gt;::type instead. The &quot;::type&quot; 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:
    > &gt; tonyk &lt;&gt; wrote:
    > &gt;&gt; Hi,
    > &gt;&gt; I'm wondering what is the most elegant way to do the following
    > &gt;&gt; Assume there are 2 or more different vendor fixed-point template classes
    > &gt;&gt; such as
    > &gt;&gt; template &lt;int T&gt; v1_int&lt;T&gt;; // always signed
    > &gt;&gt; and
    > &gt;&gt;
    > &gt;&gt; template &lt;int T, bool S&gt; v2_int&lt;T,S&gt;; // S for signed/unsigned
    > &gt;&gt; (not different template params)
    > &gt;&gt;
    > &gt;&gt; Now I'd like to create a template class
    > &gt;&gt; template &lt;int T&gt; V_int&lt;T&gt;;
    > &gt;&gt; such that with a #define or another technique have V_int represent either
    > &gt;&gt; v1_int&lt;T&gt; or v2_int&lt;T,true&gt; without having to re-write all of the member
    > &gt;&gt; interfaces, overloads, etc
    > &gt;&gt;
    > &gt;&gt; is that possible? Any advice appreciated
    > &gt;
    > &gt; If you are using C++11, you can use a template alias for that exact
    > &gt; purpose. It would be something like this:
    > &gt;
    > &gt; #ifdef SOMETHING
    > &gt; template&lt;int T&gt; using V_int = v1_int&lt;T&gt;;
    > &gt; #else
    > &gt; template&lt;int T&gt; using V_int = v2_int&lt;T, true&gt;;
    > &gt; #endif
    > &gt;
    > &gt; The above doesn't work in C++98, and I'm not sure now what would be the
    > &gt; 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&lt;int T&gt; struct V_int { typedef v1_int&lt;T&gt; type; }
    > #else
    > template&lt;int T&gt; struct V_int { typedef v2_int&lt;T,true&gt; type; }
    > #endif
    >
    > and use V_int&lt;T&gt;::type instead. The &quot;::type&quot; 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:
    > &gt; tonyk &lt;&gt; wrote:
    > &gt;&gt; Hi,
    > &gt;&gt; I'm wondering what is the most elegant way to do the following
    > &gt;&gt; Assume there are 2 or more different vendor fixed-point template classes
    > &gt;&gt; such as
    > &gt;&gt; template &lt;int T&gt; v1_int&lt;T&gt;; // always signed
    > &gt;&gt; and
    > &gt;&gt;
    > &gt;&gt; template &lt;int T, bool S&gt; v2_int&lt;T,S&gt;; // S for signed/unsigned
    > &gt;&gt; (not different template params)
    > &gt;&gt;
    > &gt;&gt; Now I'd like to create a template class
    > &gt;&gt; template &lt;int T&gt; V_int&lt;T&gt;;
    > &gt;&gt; such that with a #define or another technique have V_int represent either
    > &gt;&gt; v1_int&lt;T&gt; or v2_int&lt;T,true&gt; without having to re-write all of the member
    > &gt;&gt; interfaces, overloads, etc
    > &gt;&gt;
    > &gt;&gt; is that possible? Any advice appreciated
    > &gt;
    > &gt; If you are using C++11, you can use a template alias for that exact
    > &gt; purpose. It would be something like this:
    > &gt;
    > &gt; #ifdef SOMETHING
    > &gt; template&lt;int T&gt; using V_int = v1_int&lt;T&gt;;
    > &gt; #else
    > &gt; template&lt;int T&gt; using V_int = v2_int&lt;T, true&gt;;
    > &gt; #endif
    > &gt;
    > &gt; The above doesn't work in C++98, and I'm not sure now what would be the
    > &gt; 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&lt;int T&gt; struct V_int { typedef v1_int&lt;T&gt; type; }
    > #else
    > template&lt;int T&gt; struct V_int { typedef v2_int&lt;T,true&gt; type; }
    > #endif
    >
    > and use V_int&lt;T&gt;::type instead. The &quot;::type&quot; is the indirection.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask
    tonyk, Jul 24, 2012
    #4
  5. tonyk

    Seneika Guest

    Dear Juha and Victor, could one of you tell me whether C++11 template
    aliases are equivalent to Fortran's abstract interfaces?

    I mean, if I'm porting some code from Fortran to C++ and I use abstract
    interfaces if the former, should I search for these in C++?

    (I apologize if I violated the hierarchy of posts but it looked
    appropriate to ask my question here)

    Thanks,
    Seneika
    Seneika, Jul 25, 2012
    #5
  6. On 7/25/2012 12:25 PM, Seneika wrote:
    > Dear Juha and Victor, could one of you tell me whether C++11 template
    > aliases are equivalent to Fortran's abstract interfaces?


    I have no idea, sorry.

    > I mean, if I'm porting some code from Fortran to C++ and I use abstract
    > interfaces if the former, should I search for these in C++?


    Here is what I know. When porting from one language to another, it is
    best to seek help from people who know both languages well. It's not an
    easy feat (to find them), but I am certain that such people do exist.

    Another sentiment, speaking from experience. It's often (not always)
    better to reimplement than to blindly "translate". The main benefit of
    reimplementing is the concentrated attention you pay to the model, the
    data structures and the algorithms. It's quite rare to have an
    opportunity to do that when the codebase is stable, so use this
    interruption.

    Best of luck!

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 25, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    735
    Pete Becker
    May 4, 2005
  2. Chris
    Replies:
    2
    Views:
    669
    Chris
    Mar 4, 2007
  3. abir
    Replies:
    2
    Views:
    305
  4. Replies:
    10
    Views:
    325
    Sean O'Halpin
    Jul 17, 2006
  5. Matt H
    Replies:
    1
    Views:
    90
    Aryk Grosz
    Dec 1, 2009
Loading...

Share This Page