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

T

tonyk

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
 
J

Juha Nieminen

tonyk said:
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).
 
V

Victor Bazarov

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
 
T

tonyk

On 7/24/2012 3:33 AM, Juha Nieminen wrote:
&gt; tonyk &lt;[email protected]&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

Thanks, I will try this. Indeed this needs to work for g++ 4.2.x and i can't use C++11.
Tony

On 7/24/2012 3:33 AM, Juha Nieminen wrote:
&gt; tonyk &lt;[email protected]&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
 
S

Seneika

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
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top