2014-09-07 2 views
0
template<std::size_t sz> 
constexpr std::array<int,sz> range(){ 
    std::array<int,sz> arr{0}; 
    std::iota(arr.begin(),arr.end(),0); 
    return arr; 
} 

나는 다음과 같은 오류컴파일 타임에 어떻게 배열을 만드나요?

> $ clang++ -stdlib=libc++ -std=c++1y main.cpp -o main && ./main                     
main.cpp:33:30: error: implicit instantiation of undefined template 'std::__1::array<int, 10>' 
constexpr std::array<int,sz> range(){ 
          ^
main.cpp:48:12: note: in instantiation of function template specialization 'range<10>' requested here 
    auto r = range<10>(); 
     ^
/usr/include/c++/v1/__tuple:69:65: note: template is declared here 
template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array; 
                   ^
main.cpp:34:22: error: implicit instantiation of undefined template 'std::__1::array<int, 10>' 
    std::array<int,sz> arr{0}; 
        ^
/usr/include/c++/v1/__tuple:69:65: note: template is declared here 
template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array; 
                   ^
main.cpp:48:12: error: implicit instantiation of undefined template 'std::__1::array<int, 10>' 
    auto r = range<10>(); 
     ^
/usr/include/c++/v1/__tuple:69:65: note: template is declared here 
template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array; 
                   ^
3 errors generated. 

내 실수는 무엇인가를 얻을 수 있습니다

auto r = range<10>() 

과 같이 호출하려고하면?

+4

포함합니다. –

+1

함수는 C++ 11에서 컴파일 타임 실행에 적용되지 않습니다. – chris

+0

@chris하지만 C++ 14에서는 제대로 작동할까요? –

답변

4

것은 확실히 당신이`#INCLUDE `하지 않았다 보인다

#include <array> 
#include <numeric> 

http://coliru.stacked-crooked.com/a/fc15a6715178a49b

+0

오 소년, 당황했습니다, 고마워요. –

+0

@MaikKlein 아무 문제 없지만 문제가 해결 되었다면이 대답을 인정 된 답변으로 표시하십시오. –

관련 문제