template<class A, class B> constexpr int f(A a, B b) {
a /= b;
return a;
}
constexpr int x = f(2, 2); // a, b: int
constexpr int y = f(2., 2.); // a, b: double
constexpr int z = f(2, 2.); // a: int, b: double //<-- BOOM!
constexpr int w = f(2., 2); // a: double, b: int
int main() {}
The code doesn't compiled in clang, it produces the following diagnostic:
error: constexpr variable 'z' must be initialized by a constant expression
MSVC crashed (according to godbolt) and gcc works fine. If a /= b is simply replaced by a = a / b then everybody accepts it. Why?
Who is right? Seems it's related to implicit narrowing conversion, but then why a = a / b works?
a /= bshould be equivalent toa = a / b. - geza