constradpr 함수를 lambdas와 함께 사용할 때 문제가 발생했습니다. 다음 코드는 오류를 재현 최소한의 버전은 다음과 같습니다constexpr 및 lambda를 사용할 때의 컴파일러 오류
#include <iostream>
constexpr unsigned bar(unsigned q) {
return q;
}
template<unsigned N>
unsigned foo() {
return N;
}
template<typename F>
void print(F f) {
std::cout << f() << std::endl;
}
template<unsigned Q>
int stuff() {
constexpr unsigned n = bar(Q);
print([]() { return foo<n>(); });
}
int main() {
stuff<13>();
}
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
컴파일이 다음과 같은 컴파일러 오류입니다 :
constexpr_template.cpp: In lambda function:
constexpr_template.cpp:24:9: instantiated from ‘stuff() [with unsigned int Q = 13u]::<lambda()>’
constexpr_template.cpp:24:2: instantiated from ‘int stuff() [with unsigned int Q = 13u]’
constexpr_template.cpp:29:12: instantiated from here
constexpr_template.cpp:24:32: error: no matching function for call to ‘foo()’
constexpr_template.cpp:24:32: note: candidate is:
constexpr_template.cpp:9:10: note: template<unsigned int N> unsigned int foo()
는 constexpr unsigned n = bar(Q);
이 constexpr unsigned n = Q;
로 변경되면 이제 이상한 부분입니다 그것은 작동합니다. 또한 작동하는 것은 print([]() { return foo<bar(Q)>(); });
...
GCC의 버그입니까? 아니면 내가 뭘 잘못하고 있습니까?
나에게 맞는 것 같습니다. GCC (4.7 또는 4.8) 또는 Clang의 최신 버전을 사용해보십시오. –
clang과 작동합니다. gcc에서 동일한 오류가 발생했습니다. 4.7 – kennytm
gcc에 너무 많은 C++가 있습니다 ... – Danvil