[英]How to add const qualifier to vector<>::pointer?
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. 我试图将指向
std::vector
元素的const指针传递给函数,但似乎无法正确获得函数的签名。 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: 这是GCC(5.4)的错误消息:
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>*
. 因此,基本上,它试图将
const std::reference_wrapper<const Image>*
放入std::reference_wrapper<const Image>*
。 The function's signature has const ImageConstRefArray::pointer
as the parameter. 该函数的签名具有
const ImageConstRefArray::pointer
作为参数。 If that const
doesn't make the pointer a const pointer, then how do I write the function signature? 如果该
const
不能使该指针成为const指针,那么我该如何编写函数签名? Is the only solution to write out const std::reference_wrapper<const Image>*
? 是写出
const std::reference_wrapper<const Image>*
的唯一解决方案吗? That solves the issue, but I'd rather write it in terms of ImageConstRefArray
. 这就解决了问题,但是我宁愿用
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
). 对于
const ImageConstRefArray::pointer
, const
在指针本身上是合格的,因此它将是std::reference_wrapper<const Image>* const
(指向非const的const
指针),而不是std::reference_wrapper<const Image> const *
(指向const
非const指针)。 (Note the different position of const
.) (请注意
const
的不同位置。)
You should use std::vector::const_pointer
instead, which will give you the type of pointer to const T
. 您应该改用
std::vector::const_pointer
,它将为您提供指向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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.