one thing that bugs me about c++
guslay
guslay at gmail.com
Thu Dec 13 22:00:22 PST 2007
Neal Becker Wrote:
> --------------------
> #include <complex>
> #include <vector>
>
> using namespace std;
>
> template<typename T>
> T mag_sqr1 (T z) { return z * z; }
>
> template<typename T>
> T mag_sqr1 (complex<T> z) { return real(z)*real(z) + imag(z)*imag(z); }
>
> template<typename T>
> struct scalar { typedef T type; };
>
> template<typename T>
> struct scalar<complex<T> > { typedef T type; };
>
>
> template<typename T>
> inline vector<typename scalar<T>::type> mag_sqr (vector<T> const& z) {
> typedef typename scalar<T>::type out_t;
> vector<out_t> out (z.size());
> std::transform (z.begin(), z.end(), out.begin(), mag_sqr1<T>);
> return out;
> }
>
> int main () {
> vector<complex<double> > v;
> mag_sqr (v);
> }
The instantiation capability within std::transform appears to be the limitation. Who knows why. If you do it in two steps, by first defining a function pointer, it works fine for complex and double.
out_t (*ptrF)(T) = &mag_sqr1;
std::transform (z.begin(), z.end(), out.begin(), ptrF );
I will leave the implementation in D to someone else, but I bet it would look better.
More information about the Digitalmars-d
mailing list