Consider this program:
template <int F(), int N = F()>
void f() { }
constexpr int g() { return 1; }
int main() { f<g>(); }
Is this valid? Are compilers required to see at template definition time, that F could potentially refer to a constexpr function, and therefore the default argument for N could be valid?
gcc and clang accept this, but Intel rejects the template function at template definition time because F() is not a constant expression. Intel does accept f<g, g()>() if the default argument is removed, so clearly it understands that g() is usable in a constant expression in general.
It's not clear to me what the standard says. It's clear that (C++11 [expr.const]p2)
an invocation of a function other than a constexpr constructor for a literal class or a constexpr function
renders an expression non-constant, but it's not clear to me whether that applies here. At template definition time, it certainly does seem to apply, since F is not declared to be a constexpr function, but at the same time, errors at template definition time are supposed to be diagnosed only if there is no possible valid instantiation of the template, and there does appear to be a valid instantiation here.
I can see the arguments for both answers, so I'm getting confused. Is there a definitive answer to this question?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。