[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Problem with templated special functions

Juanjo

9/10/2008 6:37:00 PM

Hi,

I have a piece of code that looks like below. The code simply does not
compile because the compiler chooses to believe that std::cos<double>
has type signature std::complex<double> cos(std::complex<double>)

The reason I think is that there are two contradictory definitions,
one in cmath, ::using cos, and another one in complex which reads
template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);

This is with g++ 4.3. Does anybody have a solution? Am I trying the
impossible?

Juanjo

#include <cmath>
#include <complex>
#include <tensor/tensor.h>

namespace tensor {

Tensor<double> cos(const Tensor<double> &t) {
Tensor<double> output(t.dimensions());
// The iterators here have type double * and const double *
std::transform(t.begin(), t.end(), output.begin(),
std::cos<double>);
return output;
}

} // namespace tensor
1 Answer

Pete Becker

9/10/2008 6:56:00 PM

0

On 2008-09-10 14:37:26 -0400, Juanjo
<juanjose.garciaripoll@googlemail.com> said:

>
> I have a piece of code that looks like below. The code simply does not
> compile because the compiler chooses to believe that std::cos<double>
> has type signature std::complex<double> cos(std::complex<double>)

The compiler is correct.

> std::transform(t.begin(), t.end(), output.begin(),
> std::cos<double>);

Since the iterators deal in doubles, you obviously meant to refer to
the function cos that takes doubles, and were mislead by erroneous
documentation that you consulted. Make a note of it: whatever
documentation told you that the cos function that takes double is a
template should be discarded.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)