如何将常量限定符添加到 vector<>::p ointer?
How to add const qualifier to vector<>::pointer?
我正在尝试将const指针传递给 std::vector
的元素到一个函数,但我似乎无法正确地获得该函数的签名。我一定在这里缺少一些微不足道的东西,但是我很困惑。
这是重现问题的最小示例:
#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)错误消息:
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() );
基本上,它试图将const std::reference_wrapper<const Image>*
放入std::reference_wrapper<const Image>*
中。该函数的签名为const ImageConstRefArray::pointer
作为参数。如果该const
不能使指针成为const指针,那么我该如何编写函数签名?是写出const std::reference_wrapper<const Image>*
的唯一解决方案吗?这解决了问题,但我宁愿用ImageConstRefArray
来写它。
对于const ImageConstRefArray::pointer
,const
在指针本身上有资格,因此它将是std::reference_wrapper<const Image>* const
(const
指针指向非CONST),而不是std::reference_wrapper<const Image> const *
(非CONST Pointer to 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
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 如何将常量限定符添加到 vector<>::p ointer?
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 左角支架解释为操作员&lt;而不是模板参数
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- 超载操作员&lt;&lt; - 必须是二进制操作员