Consider the simple C++11 code:
template
struct Foo {};
template
constexpr int size(const Foo&) { return N; }
template
My interpretation is that clang++ is right and g++ is too permissive.
We can find a close example ([expr.const] section, page 126) in the standard https://isocpp.org/std/the-standard (draft can be downloaded, attention big PDF! ).
constexpr int g(int k) {
constexpr int x = incr(k);
return x;
}
where it is explained that:
error: incr(k) is not a core constant expression because lifetime of k began outside the expression incr(k)
This is exactly what is happening in the use_size()
function with the foo
argument, even if the size()
function only use the N
template parameter.
template
constexpr int size(const Foo&) { return N; }
template
void use_size(const Foo& foo) { constexpr int n = size(foo); }