Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to make a common template class that wraps alternate templateclasses without having to write all of the interfaces?

Reply
Thread Tools

how to make a common template class that wraps alternate templateclasses without having to write all of the interfaces?

 
 
tonyk
Guest
Posts: n/a
 
      07-23-2012
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




 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      07-24-2012
tonyk <(E-Mail Removed)> 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).
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2012
On 7/24/2012 3:33 AM, Juha Nieminen wrote:
> tonyk <(E-Mail Removed)> 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
 
Reply With Quote
 
tonyk
Guest
Posts: n/a
 
      07-24-2012
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;(E-Mail Removed)&gt; wrote:
> &gt;&gt; Hi,
> &gt;&gt; I&#39;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&#39;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&#39;t work in C++98, and I&#39;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&#39;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;(E-Mail Removed)&gt; wrote:
> &gt;&gt; Hi,
> &gt;&gt; I&#39;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&#39;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&#39;t work in C++98, and I&#39;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&#39;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;(E-Mail Removed)&gt; wrote:
> &gt;&gt; Hi,
> &gt;&gt; I&#39;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&#39;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&#39;t work in C++98, and I&#39;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&#39;t ask


 
Reply With Quote
 
Seneika
Guest
Posts: n/a
 
      07-25-2012
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-25-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
what's 'alternate' in <link rel=alternate>? Chris ASP .Net 2 03-04-2007 04:45 PM
Alternate initializers or alternate class? transfire@gmail.com Ruby 10 07-17-2006 03:20 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments