Implicit conversion from cuFloatComplex to cuDoubleComplex? user-defined conversions

Is there a way to define an implicit upcast from cuFloatComplex to cuDoubleComplex?

I’m not good at C++, but don’t I have to write a constructor for the cuDoubleComplex class that takes in a cuFloatComplex? What would that look like this:
cuDoubleComplex::cuDoubleComplex(cuFloatComplex c) x(c.x), y(c.y) {}
…but I know that’s wrong because the compiler complains.

My problem: I’m writing single/double-precision versions of several complex valued functions. For each function, I would like to write one double-precision version and one single-precision version. However, for functions that take in two inputs, I don’t want to have to write the single/double and double/single versions. I would rather write just single/single and double/double, and if the user inputs single/double then it would upcast and feed it into the double/double version.

Try passing “cuFloatComplex” as a const reference… May b, your stuff is leading to recursive construction.

cuDoubleComplex::cudDoubleComplex(cuFloatComplex const& c) x(c.x),y(c.y){}


cuDoubleComplex::cudDoubleComplex(cuFloatComplex &c) x(c.x),y(c.y){}

I’ve tried various things with no success, several failed attempts are in the code snippet below.

This has got to be trivial: implicitly convert cuFloatComplex to cuDoubleComplex.

Using the code below, could someone post such a conversion function?


#include <cuComplex.h>

#include <stdio.h>


cuDoubleComplex(cuFloatComplex a) { cuDoubleComplex a; a.x = x; a.y = y; return a; }

cuDoubleComplex::cuDoubleComplex(cuFloatComplex const& c) x(c.x),y(c.y){}

cuDoubleComplex::cuDoubleComplex(cuFloatComplex const& c) x(c.x),y(c.y){}

operator cuDoubleComplex::double2(cuFloatComplex const &c) x(c.x), y(c.y) {}

void cuDoubleComplex(const cuDoubleComplex &a, cuFloatComplex const b)


	a.x = b.x;

	a.y = b.y;



/* add real and imaginary components */

double foo(cuDoubleComplex a) { return cuCreal(a) + cuCimag(a); }

int main(void)


	cuFloatComplex a = make_cuFloatComplex(1,2);

	printf("%f\n", foo(a));

	return 0;


You cannot just add a constructor or member function outside of the class definition. It will always raise a compile error.

However I found the following in cuComplex.h file:

/* float-to-double promotion */

__host__ __device__ static __inline__ cuDoubleComplex cuComplexFloatToDouble

													  (cuFloatComplex c)


	return make_cuDoubleComplex ((double)cuCrealf(c), (double)cuCimagf(c));


__host__ __device__ static __inline__ cuFloatComplex cuComplexDoubleToFloat

(cuDoubleComplex c)


	return make_cuFloatComplex ((float)cuCreal(c), (float)cuCimag(c));