Skip to content

<complex>: pow(complex<float>, int) returns complex<float> instead of complex<double> #325

@amyw-msft

Description

@amyw-msft

The Standard states that pow(complex<float>, int) should treat both arguments as complex<double>, indicating that it should also return complex<double>. Instead it returns a complex<float>.

> type test.cpp

#include <complex>
#include <type_traits>

using namespace std;

int main()
{
    static_assert(
        is_same_v<
            complex<double>,
            decltype(pow(complex<float>{}, int{}))
        >,
        "pow(complex<float>, int) should return complex<double>"
        );
}

> cl /EHsc /nologo /W4 /std:c++17 test.cpp
test.cpp
test.cpp(12): error C2338: pow(complex<float>, int) should return complex<double>

WG21-N4835 [cmplx.over]/3 states:

Function template pow shall have additional overloads sufficient to ensure, for a call with at least one argument of type complex<T>:

  • If either argument has type complex<long double> or type long double, then both arguments are effectively cast to complex<long double>.
  • Otherwise, if either argument has type complex<double>, double, or an integer type, then both arguments are effectively cast to complex<double>.
  • Otherwise, if either argument has type complex<float> or float, then both arguments are effectively cast to complex<float>.

libc++ will return the correct type. libstdc++ will not. https://godbolt.org/z/_TscRV

Also tracked by Microsoft-internal VSO-177118.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions