Topic: Dear All,
Author: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Wed, 28 Apr 2004 05:17:28 +0000 (UTC) Raw View
[to moderater: my first post seem to be lost]
Dear All,
Sometimes one choices to change the result of a function template to a
parameter to get type deduction.
For example,
template< class Vector, class Matrix >
void mean_vector( const Matrix&, Vector& );
Matrix m;
Vector v;
...
mean_vector( m, v );
this interacts poorly with a functional style of programming where we would
have written
template< class Vector, class Matrix >
Vector mean_vector( const Matrix& );
Matrix m;
...
Vector v = mean_vector<Vector>( m );
The template argument above seems redundant; why shouldn't we be able to say
Vector v = mean_vector( m );
as if mean_vector() returns a proxy with all possible conversion operators
defined ?
remark: given a templated conversion operator in a proxy we could return the
proxy from mean_vector()
to make it work. However, that incurs a large overhead.
br
Thorsten
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Michiel.Salters@logicacmg.com (Michiel Salters)
Date: Wed, 28 Apr 2004 15:57:32 +0000 (UTC) Raw View
nesotto@cs.auc.dk ("Thorsten Ottosen") wrote in message news:<408df3b1$0$436$afc38c87@news.optusnet.com.au>...
[propoped]
> template< class Vector, class Matrix >
> Vector mean_vector( const Matrix& );
>
> Matrix m;
> Vector v = mean_vector( m );
>
> as if mean_vector() returns a proxy with all possible conversion operators
> defined ?
>
> remark: given a templated conversion operator in a proxy we could
> return the proxy from mean_vector()
> to make it work. However, that incurs a large overhead.
Why would there be a large overhead?
template< typename Matrix >
class mean_vector_return<Matrix> {
friend mean_vector_return( Matrix const& argument );
Matrix const& argument;
public:
template< typename Vector >
operator Vector() { /* real implementation */ }
};
template< typename Matrix >
inline mean_vector_return<Matrix> mean_vector( Matrix const& argument )
{
return mean_vector_return<Matrix>( argument );
}
Basically, everything except the operator T could be inlined. The
operator Vector actually implements the intended
mean_vector<Matrix,Vector>() function.
Regards,
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Fri, 30 Apr 2004 03:14:41 +0000 (UTC) Raw View
"Michiel Salters" <Michiel.Salters@logicacmg.com> wrote in message
news:fcaee77e.0404280424.54399c9a@posting.google.com...
[snip]
| Why would there be a large overhead?
| template< typename Matrix >
| class mean_vector_return<Matrix> {
| friend mean_vector_return( Matrix const& argument );
| Matrix const& argument;
| public:
| template< typename Vector >
| operator Vector() { /* real implementation */ }
| };
I see, clever :-)
| template< typename Matrix >
| inline mean_vector_return<Matrix> mean_vector( Matrix const& argument )
| {
| return mean_vector_return<Matrix>( argument );
| }
| Basically, everything except the operator T could be inlined. The
| operator Vector actually implements the intended
| mean_vector<Matrix,Vector>() function.
I guess it works then, but it leaves some issues:
1. it seems quite strange to put the implementation in a conversion operator
instead of in the function body
2. all function prototypes would have to look like
template< class T >
foo_return<T> foo( T const& );
br
Thorsten
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]