함수에 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
으로 작성해야합니다.
'const ImageConstRefArray :: value_type *'또는'ImageConstRefArray :: const_pointer'. – AnT
@AnT Doh! 정말 사소한 일입니다. C++이 나를 미치게 만들고있다! 감사! –