I'm trying to pass a const pointer to an element of a std::vector
to a function, but I can't seem to get the function's signature right. I must be missing something trivial here, but I'm confused.
This is the minimal example that reproduces the issue:
#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 );
}
This is GCC's (5.4) error message:
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() );
So basically it's trying to put a const std::reference_wrapper<const Image>*
into a std::reference_wrapper<const Image>*
. The function's signature has const ImageConstRefArray::pointer
as the parameter. If that const
doesn't make the pointer a const pointer, then how do I write the function signature? Is the only solution to write out const std::reference_wrapper<const Image>*
? That solves the issue, but I'd rather write it in terms of ImageConstRefArray
.
For const ImageConstRefArray::pointer
, const
is qualifed on the pointer itself, so it'll be std::reference_wrapper<const Image>* const
( const
pointer to non-const), but not std::reference_wrapper<const Image> const *
(non-const pointer to const
). (Note the different position of const
.)
You should use std::vector::const_pointer
instead, which will give you the type of pointer to const T
. eg
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.