2013-08-14 1 views
2

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의 버그입니까? 아니면 내가 뭘 잘못하고 있습니까?

+0

나에게 맞는 것 같습니다. GCC (4.7 또는 4.8) 또는 Clang의 최신 버전을 사용해보십시오. –

+0

clang과 작동합니다. gcc에서 동일한 오류가 발생했습니다. 4.7 – kennytm

+0

gcc에 너무 많은 C++가 있습니다 ... – Danvil

답변

0

Gcc 4.6은 the first version to support constexpr이며, 이러한 기능이 해제 될 때 사소한 버그가있는 것은 드문 일이 아닙니다. from this Live Example on Coliru gcc 4.8.1 및 Clang 3.4 SVN이 코드를 올바르게 구문 분석하는지 확인할 수 있습니다. 아마도 컴파일러를 업그레이드해야합니다.

+0

이 경우 발생하는 경우에 싫습니다 ... – Danvil

+0

@Danvil pre-C++ 11 표준 컴파일러 사용시 기대했던 점은 무엇입니까? curent gcc 4.8은 C++ 14와 비슷하게 동작하므로 3 년 동안 버전 또한 고대입니다 .-) 내 경험적 규칙 : gcc 4.6 = alpha, 4.7 = beta, 4.8 = rtm – TemplateRex

관련 문제