Narrowing, unevaluated context and template function
Asked Answered
K

0

7

Consider the following code:

auto f() -> decltype(int{0.}, void()) { }
int main() { f(); }

It doesn't compile (as expected) with an error:

narrowing conversion of '0.0' from 'double' to 'int' inside { }

Both GCC and clang agree on that.
Now consider the code below:

template <typename T>
auto f(T) -> decltype(int{0.}, void()) { }

int main(){
    f(0);
}

In this case, clang 3.9 returns an error and GCC 6.2 compiles with no errors.

Is there any reason for which the narrowing conversion should be accepted in case of a function template or is it an error of GCC?


I'm going to open an issue to GCC for I guess it should fail to compile, but I would like to know if I'm missing something important about function template here.

Kavanagh answered 7/11, 2016 at 23:14 Comment(4)
Definite GCC bug, although without that use of f it would be ill-formed NDR.Crackerjack
@Crackerjack I don't get it. Why would it be ill-formed?Kavanagh
[temp.res]/8, as usual.Crackerjack
Now all compilers issue an error with your second template example: gcc.godbolt.org/z/8vPjqffTsJughead

© 2022 - 2024 — McMap. All rights reserved.