Both compilers agree that get_x()
cannot be used in constant expression. You can tell by changing auto res = get_x();
on constexpr auto res = get_x();
where the GCC will reject it equally.
As for defining it in defining a function, as clang does, and not in a function using gcc, both are allowed: (my selection)
7.1.5 constexpr specifier [dcl.constexpr]
5 For a non-symbolic, non-default constexpr
function or constructor without a non-default template, without constexpr
inheritance, if there are no argument values, so that the function or constructor call can be evaluated by subexpressing the main constant expression (5.19), the program is poorly formed; no diagnostics required . [...]
In the general case, it is impossible to reliably determine whether a function call exists that allows using the result in a constant expression; therefore, diagnostics are optional.
source share