Can `if constexpr` be used to declare variables with different types and init-expr

Viewed 3071

For example:

void foo()
{
    if constexpr (...)
        int x = 5;
    else
        double x = 10.0;
    bar(x); // calls different overloads of bar with different values
}

It's common case in D lang, but I didn't found info about C++17.

Of course, it is possible to use something like

std::conditional<..., int, double>::type x;

but only in elementary cases. Even different initializators (as above) creates big problem.

2 Answers
Related