【发布时间】:2013-09-07 06:12:43
【问题描述】:
我使用这个代码来找到一个盒子的点 (g) 在方向上最远 d
typedef vector_t point_t;
std::vector<point_t> corners = g.getAllCorners();
coordinate_type last_val = 0;
std::vector<point_t>::const_iterator it = corners.begin();
point_t last_max = *it;
do
{
coordinate_type new_val = dot_product( *it, d );
if( new_val > last_val )
{
last_val = new_val;
last_max = *it;
}
}
while( it != corners.end() );
return last_max;
对于vector_t 的类vector_t 的运算符!=,我还有一个模板运算符重载,它位于命名空间point 中。
namespace point
{
template
<
typename lhs_vector3d_impl,
typename rhs_vector3d_impl
>
bool operator!=( const typename lhs_vector3d_impl& lhs, const typename rhs_vector3d_impl& rhs )
{
return binary_operator_not_equal<lhs_vector3d_impl, rhs_vector3d_impl>::apply( lhs, rhs );
}
};
重载在大多数情况下都可以正常工作,但是当我使用迭代器(即it != corners.end())时,它会崩溃,因为在这种情况下我不打算使用此函数。
我可以说这是因为模板参数解析出错但我不知道为什么:
lhs_vector3d_impl=std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<legend::geometry::point::Carray_Vector3d<int32_t>>>>,
rhs_vector3d_impl=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<legend::geometry::point::Carray_Vector3d<int32_t>>>>
我知道调用了错误的函数,但我不明白为什么……
所以基本上我的问题是如何用我的函数而不是 std 命名空间中的运算符解决 comme 迭代器比较,以及如何防止使用此函数。
注意1:我是从模板开始的,所以我可能在不知不觉中做错了什么,如果是,请告诉。
注意 2:此代码主要用于学术目的,所以我真的很想手动完成大部分工作。
注意 3:使用 Visual Studio 2012 C++ 编译器
【问题讨论】:
-
(A) 你可能写了
using namespace point所以一切都看到了过载。 (B) 重载是template<class A, class B> bool operator(const A&,const B&),它匹配所有类型。不要在这里使用模板。有一些变通方法,但它们令人困惑。 -
即使没有
using namespace,它也会通过 ADL 的魔力被拾取 - 这不是你想要的。
标签: c++ templates stl iterator operator-overloading