Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Return type autodetection in templates

Reply
Thread Tools

Return type autodetection in templates

 
 
Tim Wesson
Guest
Posts: n/a
 
      07-08-2003
Does anyone know if it is possible to choose between the below
templates (or similar ones) according to the promotion rules for
types T and U?

Thanks,

Tim Wesson.

[...]

template<typename T, typename U>
inline matrix<T> operator+(const matrix<T>& m, const matrix<U>& n)
{ return matrix<T> (m) += n; }

template<typename T, typename U>
inline matrix<U> operator+(const matrix<T>& m, const matrix<U>& n)
{ return matrix<U> (m) += n; }

int main (void)
{
matrix<double> matd1, matd2;
matrix<float> matf1, matf2;

cout << matd1 * matf1 << '\n'; // should print matrix<double> ...
cout << matf2 * matd2 << '\n'; // should print matrix<double> ...
}
 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      07-08-2003
In article <(E-Mail Removed)> , Tim Wesson
<(E-Mail Removed)> wrote:

| Does anyone know if it is possible to choose between the below
| templates (or similar ones) according to the promotion rules for
| types T and U?
|
| Thanks,
|
| Tim Wesson.
|
| [...]
|
| template<typename T, typename U>
| inline matrix<T> operator+(const matrix<T>& m, const matrix<U>& n)
| { return matrix<T> (m) += n; }
|
| template<typename T, typename U>
| inline matrix<U> operator+(const matrix<T>& m, const matrix<U>& n)
| { return matrix<U> (m) += n; }
|
| int main (void)
| {
| matrix<double> matd1, matd2;
| matrix<float> matf1, matf2;
|
| cout << matd1 * matf1 << '\n'; // should print matrix<double> ...
| cout << matf2 * matd2 << '\n'; // should print matrix<double> ...
| }

You can sort of do this today. But it doesn't scale well if you have
lots of different types to combine. For just float and double you
might try something like:

template <class T, class U> struct promote;
template <> struct promote<double, double> {typedef double type;};
template <> struct promote<double, float> {typedef double type;};
template <> struct promote<float, double> {typedef double type;};
template <> struct promote<float, float> {typedef float type;};

template<typename T, typename U>
inline
matrix<typename promote<T,U>::type>
operator+(const matrix<T>& m, const matrix<U>& n)
{
matrix<typename promote<T,U>::type> temp(m);
temp += n;
return temp;
}

This also shows a potential optimization: Assuming matrix:perator+=
returns a matrix&, don't return that. Instead return the temp directly
as shown above. The former disables return value optimization. The
latter does not.

If you happen to be running on gnu, or Metrowerks CodeWarrior, you
might try __typeof__:

template<typename T, typename U>
inline
matrix<__typeof__(T() + U())>
operator+(const matrix<T>& m, const matrix<U>& n)
{
matrix<__typeof__(T() + U())> temp(m);
temp += n;
return temp;
}

This scales better, but isn't standard. However the committee is hotly
debating a typeof-like animal for C++0X.

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
 
 
 
Tim Wesson
Guest
Posts: n/a
 
      07-09-2003
Howard Hinnant <(E-Mail Removed)> wrote in message news:<080720031112342430%(E-Mail Removed)>.. .
> In article <(E-Mail Removed)> , Tim Wesson
> <(E-Mail Removed)> wrote:
>
> | Does anyone know if it is possible to choose between the below
> | templates (or similar ones) according to the promotion rules for
> | types T and U?
> |
> | Thanks,
> |
> | Tim Wesson.
> |
> | [...]
> |
> | template<typename T, typename U>
> | inline matrix<T> operator+(const matrix<T>& m, const matrix<U>& n)
> | { return matrix<T> (m) += n; }
> |
> | template<typename T, typename U>
> | inline matrix<U> operator+(const matrix<T>& m, const matrix<U>& n)
> | { return matrix<U> (m) += n; }
> |
> | int main (void)
> | {
> | matrix<double> matd1, matd2;
> | matrix<float> matf1, matf2;
> |
> | cout << matd1 * matf1 << '\n'; // should print matrix<double> ...
> | cout << matf2 * matd2 << '\n'; // should print matrix<double> ...
> | }
>
> You can sort of do this today. But it doesn't scale well if you have
> lots of different types to combine. For just float and double you
> might try something like:
>
> template <class T, class U> struct promote;
> template <> struct promote<double, double> {typedef double type;};
> template <> struct promote<double, float> {typedef double type;};
> template <> struct promote<float, double> {typedef double type;};
> template <> struct promote<float, float> {typedef float type;};
>
> template<typename T, typename U>
> inline
> matrix<typename promote<T,U>::type>
> operator+(const matrix<T>& m, const matrix<U>& n)
> {
> matrix<typename promote<T,U>::type> temp(m);
> temp += n;
> return temp;
> }
>
> This also shows a potential optimization: Assuming matrix:perator+=
> returns a matrix&, don't return that. Instead return the temp directly
> as shown above. The former disables return value optimization. The
> latter does not.
>
> If you happen to be running on gnu, or Metrowerks CodeWarrior, you
> might try __typeof__:
>
> template<typename T, typename U>
> inline
> matrix<__typeof__(T() + U())>
> operator+(const matrix<T>& m, const matrix<U>& n)
> {
> matrix<__typeof__(T() + U())> temp(m);
> temp += n;
> return temp;
> }
>
> This scales better, but isn't standard. However the committee is hotly
> debating a typeof-like animal for C++0X.


Thankyou, this is as good an answer as I could hope for. I'm just getting
to grips with C++, hence the attempt to write a generic matrix program.
 
Reply With Quote
 
Tim Wesson
Guest
Posts: n/a
 
      07-19-2003
On Wed, 09 Jul 2003 07:09:50 -0700, Tim Wesson wrote:

> Howard Hinnant <(E-Mail Removed)> wrote in message news:<080720031112342430%(E-Mail Removed)>.. .


[...]

>> If you happen to be running on gnu, or Metrowerks CodeWarrior, you
>> might try __typeof__:
>>
>> template<typename T, typename U>
>> inline
>> matrix<__typeof__(T() + U())>
>> operator+(const matrix<T>& m, const matrix<U>& n)
>> {
>> matrix<__typeof__(T() + U())> temp(m);
>> temp += n;
>> return temp;
>> }
>>
>> This scales better, but isn't standard. However the committee is hotly
>> debating a typeof-like animal for C++0X.

>
> Thankyou, this is as good an answer as I could hope for. I'm just getting
> to grips with C++, hence the attempt to write a generic matrix program.


Sadly this doesn't work using g++. Still, nice to know it's in the
pipeline!

http://gcc.gnu.org/PR11578

Cheers,

Tim Wesson

 
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
templates, typename, and basic_streambuf method return type problems Christopher C++ 1 01-29-2008 07:11 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
proxy autodetection lux Python 0 09-09-2004 03:13 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
FOAF autodetection Benny XML 0 10-05-2003 10:02 PM



Advertisments