Date: Mon, 20 Nov 2023 09:14:00 -0500

On Sun, Nov 19, 2023 at 9:34 PM Paul Caprioli via Std-Proposals <

std-proposals_at_[hidden]> wrote:

> I'd like to float the idea of complex type traits, specifically

> `add_complex_t<T>`, `remove_complex_t<T>`, and the boolean

> `is_complex_v<T>`.

>

You can do the first two today easily, and the third is just another

special-case of `is_specialization_of`.

template<class T> using add_complex_t = std::complex<T>;

template<class T> using remove_complex_t = typename T::value_type;

template<class T> inline constexpr bool is_complex_v =

std::is_specialization_of<T, std::complex>; // fantasy syntax

A motivating example might by `axpy()`, which multiplies the real-valued

> scalar `a` to each element of the array `x` and adds this to the array

> `y`. The array elements can be, say, `float` or `std::complex<float>`, but

> the scalar `a` has to be `float`. Note that the arrays can be arrays of

> real values or arrays of complex values, and if complex values, the type

> can still be `float*` with the understanding that the real and imaginary

> parts are interleaved. The same implementation works. If `a` were

> complex, then a different implementation would be needed.

>

I don't see how those type-traits are going to help at all with your

type-punning problem. For that, all you want is

template<class T>

void axpy(T a, T *x, T *y, int n) {

for (int i=0; i < n; ++i)

y[i] += a * x[i];

}

int main() {

float fa[10], fb[10];

std::complex<float> ca[10], cb[10];

static_assert(sizeof(ca) == 2*sizeof(fa));

axpy(3.14, fa, fb, 10);

axpy(3.14, (float*)ca, (float*)cb, 20);

}

â€“Arthur

std-proposals_at_[hidden]> wrote:

> I'd like to float the idea of complex type traits, specifically

> `add_complex_t<T>`, `remove_complex_t<T>`, and the boolean

> `is_complex_v<T>`.

>

You can do the first two today easily, and the third is just another

special-case of `is_specialization_of`.

template<class T> using add_complex_t = std::complex<T>;

template<class T> using remove_complex_t = typename T::value_type;

template<class T> inline constexpr bool is_complex_v =

std::is_specialization_of<T, std::complex>; // fantasy syntax

A motivating example might by `axpy()`, which multiplies the real-valued

> scalar `a` to each element of the array `x` and adds this to the array

> `y`. The array elements can be, say, `float` or `std::complex<float>`, but

> the scalar `a` has to be `float`. Note that the arrays can be arrays of

> real values or arrays of complex values, and if complex values, the type

> can still be `float*` with the understanding that the real and imaginary

> parts are interleaved. The same implementation works. If `a` were

> complex, then a different implementation would be needed.

>

I don't see how those type-traits are going to help at all with your

type-punning problem. For that, all you want is

template<class T>

void axpy(T a, T *x, T *y, int n) {

for (int i=0; i < n; ++i)

y[i] += a * x[i];

}

int main() {

float fa[10], fb[10];

std::complex<float> ca[10], cb[10];

static_assert(sizeof(ca) == 2*sizeof(fa));

axpy(3.14, fa, fb, 10);

axpy(3.14, (float*)ca, (float*)cb, 20);

}

â€“Arthur

Received on 2023-11-20 14:14:13