2014-02-12 1 views
4

Avoiding struct in variadic template function에 제시된 솔루션을 필자의 필요에 맞게 적용하려고합니다. 그러나 G ++의 동작을 이해할 수는 없습니다. 내가 unsigned long Size 또는 size_tunsigned Size를 변경하는 경우가 일치하지만non size_t 정수가있는 std :: array에 대한 C++ 템플릿 매개 변수 공제

template <typename T, unsigned Size> 
int nextline(const typename std::array<T, Size> ar) { 
    return 0; 
} 

그런 다음 호출

nextline(std::array<int, 2> { 1,0 }); 

는 GCC와 일치하지 않는

eslong.cpp: In function ‘int main()’: 
eslong.cpp:10:38: error: no matching function for call to ‘nextline(std::array<int, 2ul>)’ 
    nextline(std::array<int, 2> { 1,0 }); 
            ^
eslong.cpp:10:38: note: candidate is: 
eslong.cpp:4:5: note: template<class T, unsigned int Size> int nextline(std::array<T, Size>) 
int nextline(const typename std::array<T, Size> ar) { 
    ^
eslong.cpp:4:5: note: template argument deduction/substitution failed: 
eslong.cpp:10:38: note: mismatched types ‘unsigned int’ and ‘#‘integer_cst’ not supported by dump_type#<type error>’ 
    nextline(std::array<int, 2> { 1,0 }); 
            ^
eslong.cpp:10:38: note: ‘std::array<int, 2ul>’ is not derived from ‘std::array<T, Size>’ 

과 불평 : 다음 함수를 고려하십시오. 나는 여기서 무슨 일이 일어나고 있는지 이해할 수 없다. std::array<T, Size>에 대한 호출의 Size 매개 변수가 size_t으로 변환되지 않았습니까?

+0

그것은 나를 위해 잘 컴파일 : http://ideone.com/1FEmgZ – yizzlez

+1

1)'typename' 여기 ('const typename std :: array ....')에 잘못되었습니다. 2) 비 형식 템플릿 매개 변수의 유형과 정확히 일치해야합니다. [temp.deduct.type]/17을 참조하십시오. – dyp

답변

6

std::array는 다음과 같이 템플릿된다. 그러나 함수에서 부호없는 (int)을 전달하면 size_t으로 해석 할 수 없습니다. SFINAE에 따르면 템플릿을 차감 할 수 없으면 존재하지 않으므로 템플릿 기능이 존재하지 않습니다.

이것은 전화 회선에는 문제가 아니지만 기능 템플릿에 대한 선언입니다. 심지어 사용,이 경우

template <typename T, size_t Size> 
int nextline(const typename std::array<T, Size> ar) { 
    return 0; 
} 

:이 공제하고 주조 할 수 있기 때문에

nextline(std::array<int, 2ul> { 1,0 }); 

그것은 여전히 ​​작동이 문제를 해결하려면 올바른 유형을 사용합니다. dyp에 의해


추가 설명 :

이 에게 는
에게 이

가 [temp.deduct.type] 연역 된 물건의 유형 (템플릿 인수)가 필요 비 형 템플릿 매개 변수에 대한/17가 될 이 템플릿 매개 변수는 추론되는 템플릿 매개 변수와 동일한 유형의 매개 변수입니다.

0

리터럴 2unsigned long으로 해석되지만 Size 템플릿은 unsigned int으로 선언됩니다. 그냥이 대신 사용 크기 N이 유형 size_t이 요구되는 동안

template<class T, std::size_t N > struct array; 

:

template <typename T, size_t Size> 
관련 문제