2017-12-22 43 views
1

함수에 std::vector의 요소에 const 포인터를 전달하려고하는데 함수의 서명을 올바르게 가져올 수 없습니다. 나는 여기서 사소한 것을 놓치고 있어야하지만 나는 혼란 스럽다.const 한정자를 vector <> :: pointer에 추가하는 방법?

이 문제를 재현 최소한의 예입니다 : std::reference_wrapper<const Image>*const std::reference_wrapper<const Image>*를 넣어하려고 그러니까 기본적으로

test.cpp: In instantiation of ‘void Function(const ImageConstRefArray&) [with Types = {}; ImageConstRefArray = std::vector<std::reference_wrapper<const Image> >]’: 
test.cpp:24:20: required from here 
test.cpp:18:28: error: no matching function for call to ‘TestDataType(const std::reference_wrapper<const Image>*)’ 
    TestDataType<Types...>(images.data()); 
          ^
test.cpp:9:6: note: candidate: template<class T, class ... OtherTs> void TestDataType(std::vector<std::reference_wrapper<const Image> >::pointer) 
void TestDataType(const ImageConstRefArray::pointer images) { 
    ^
test.cpp:9:6: note: template argument deduction/substitution failed: 
test.cpp:18:41: note: cannot convert ‘(& images)->std::vector<_Tp, _Alloc>::data<std::reference_wrapper<const Image>, std::allocator<std::reference_wrapper<const Image> > >()’ (type ‘const std::reference_wrapper<const Image>*’) to type ‘std::vector<std::reference_wrapper<const Image> >::pointer {aka std::reference_wrapper<const Image>*}’ 
    TestDataType<Types...>(images.data()); 

:

#include <vector> 
#include <functional> 

class Image { void* ptr; }; 

using ImageConstRefArray = std::vector< std::reference_wrapper< Image const >>; 

template< typename T = void, typename... OtherTs > 
void TestDataType(const ImageConstRefArray::pointer images) { 
    // stuff. 
    TestDataType<OtherTs...>(images + 1); 
} 
template<> 
inline void TestDataType<>(const ImageConstRefArray::pointer /*images*/) {} // End of iteration 

template< typename... Types > 
void Function(ImageConstRefArray const& images) { 
    TestDataType<Types...>(images.data()); 
} 

int main() { 
    Image img1, img2; 
    ImageConstRefArray array{ img1, img2 }; 
    Function(array); 
} 

이 GCC의 (5.4) 오류 메시지입니다. 함수의 서명은 const ImageConstRefArray::pointer입니다. 그 const 포인터 포인터를 const 포인터로 만들지 않으면 어떻게 함수 서명을 작성합니까? const std::reference_wrapper<const Image>*을 쓸 수있는 유일한 솔루션입니까? 문제가 해결되었지만 ImageConstRefArray으로 작성해야합니다.

+0

'const ImageConstRefArray :: value_type *'또는'ImageConstRefArray :: const_pointer'. – AnT

+0

@AnT Doh! 정말 사소한 일입니다. C++이 나를 미치게 만들고있다! 감사! –

답변

2

const ImageConstRefArray::pointer를 들어, const 포인터 자체에 유자격되고, 그래서 std::reference_wrapper<const Image>* const (const가 아닌 const 포인터),하지만 std::reference_wrapper<const Image> const * (const에 const가 아닌 포인터) 수 있습니다. const의 다른 위치에 유의하십시오.

대신 std::vector::const_pointer을 사용해야합니다. const T에 대한 포인터 유형을 제공합니다. 예 :

template< typename T = void, typename... OtherTs > 
void TestDataType(ImageConstRefArray::const_pointer images) { 
    // stuff. 
    TestDataType<OtherTs...>(images + 1); 
} 
template<> 
inline void TestDataType<>(ImageConstRefArray::const_pointer /*images*/) {} // End of iteration 
+0

그러나 왜'vector :: pointer'가'vector :: value_type *'인지,'const vector :: pointer'가'const vector :: value_type *'로 변환되지 않는 이유는 무엇입니까? –

+0

@CrisLuengo Answer revised. – songyuanyao

+0

고마워, 나는 그것을 본 것 같아. 그리고 그것은 일종의 의미가 있습니다. 재밌는 당신이 어떻게 언어를 이해한다고 생각하지만 예기치 않은 일들로 계속 돌아옵니다. :/ –