【发布时间】:2018-12-30 13:29:06
【问题描述】:
考虑以下代码:
template <int N, typename T> void f(T) { }
template <typename T>
constexpr int k(T&) { return 0; }
int main()
{
constexpr auto i = 1;
f<k(i)>([&i]
{
f<k(i)>(0);
});
}
clang++ (trunk) 编译它。 g++ (trunk) 失败并出现以下错误:
<source>: In lambda function: <source>:11:19: error: no matching function for call to 'f<k<const int>((* & i))>(int)' 11 | f<k(i)>(0); | ^ <source>:1:35: note: candidate: 'template<int N, class T> void f(T)' 1 | template <int N, typename T> void f(T) { } | ^ <source>:1:35: note: template argument deduction/substitution failed: <source>:11:19: error: '__closure' is not a constant expression 11 | f<k(i)>(0); | ^ <source>:11:13: note: in template argument for type 'int' 11 | f<k(i)>(0); | ~^~~
将k(T&) 更改为k(T) 可解决此问题。在我看来,问题与引用参数不是常量表达式这一事实有关,但它没有用作k 的一部分。
这里什么编译器是正确的?
【问题讨论】:
-
相关:GCC and Clang disagree about C++17 constexpr lambda captures。 T.C. 的回答也回答了你的问题。
标签: c++ templates language-lawyer c++17 constexpr