解决运算符歧义
我有以下映射类型...
std::map<D3DXCOLOR, ID3DXMesh*>
在编译期间,xfunction 抱怨它无法解决有关键类型的歧义;
error C2593: 'operator <' is ambiguous
编译器检测到的候选运算符如下;
- 内置 C++ 运算符<(DWORD, DWORD)
- 内置 C++ 运算符<(FLOAT, FLOAT)
- 内置 C++ 运算符<(D3DCOLORVALUE, D3DCOLORVALUE)
D3DXCOLOR 结构由 4 个浮点数组成 r, <分别是strong>g、b和a,但没有定义运算符<。然而,它确实提供了 DWORD FLOAT 和 D3DCOLORVALUE 的转换函数,因此也提供了候选列表中的条目。
我正在考虑解决这个问题的最佳方法。我可以为 D3DXCOLOR 编写自己的内联运算符,将颜色包装在一个提供自己的运算符 < 的新类中,或者是否可以以某种方式提示编译器应从候选列表中选择哪个实现? DWORD 运算符 <会充分满足我的要求。
I have the following map type...
std::map<D3DXCOLOR, ID3DXMesh*>
During compilation, xfunctional complains that it cannot resolve an ambiguity regarding the key type;
error C2593: 'operator <' is ambiguous
The candidate operators detected by the compiler are as follows;
- built-in C++ operator<(DWORD, DWORD)
- built-in C++ operator<(FLOAT, FLOAT)
- built-in C++ operator<(D3DCOLORVALUE, D3DCOLORVALUE)
The D3DXCOLOR struct consists of 4 floats r, g, b, and a respectively but does not define a operator <. It does however provide cast functions for DWORD FLOAT and D3DCOLORVALUE, hence the entries in the candidate list.
I am contemplating the best way to resolve this problem. I could write my own inline operator for D3DXCOLOR, wrap the colour inside a new class which provides its own operator <, or is it possible to somehow hint to the compiler which implementation should be chosen from the list of candidates? The DWORD operator < would meet my requirements adequately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你有三个选择。例如,假设您希望将它们作为颜色值进行比较:
1)定义
operator<
:2)专门化
std :: less
:3)为您的地图提供第三个模板参数 -请注意,这会更改地图的类型,因此如果您经常传递地图,这可能会很不方便。但它表示该顺序仅用于此地图,而不是用于任何其他目的的规范“正确”颜色顺序。
You have three options. Supposing for example that you want them compared as colorvalues:
1) Define
operator<
:2) Specialize
std::less
:3) Supply a third template parameter to your map - note that this changes the type of the map, so if you pass the map around a lot this might be inconvenient. But it expresses that the ordering is to be used only for this map, not as the canonical "correct" order of colors for any other purpose.
您只需将小于函子传递给应该使用的
map
类模板即可。这绝对是我在这种情况下会做的事情,除非在其他情况下您还需要此类的
运算符 <
。You can just pass a less-than functor to the
map
class template that should be used.This is definitely what I would do in this case, unless you also need the
operator <
for this class in other cases.您需要编写自己的运算符<,或者为映射提供比较器函子。
You'll need to write your own operator<, or provide a comparator functor to the map.
为
D3DXCOLOR
定义operator<
函数,为或定义一个 compare 函子,称为
D3DXCOLOR_LESS
并将其作为第三个传递std::map
的参数:Define
operator<
function forD3DXCOLOR
, asOr define a compare functor, something called
D3DXCOLOR_LESS
and pass it as third parameter to thestd::map
:实际上,RGBA 颜色没有像任何标量那样的默认准顺序。并且您不应该在全局上下文中定义一个顺序,但您可以定义自己的顺序并在 std::map 模板实例中指定它。请参阅参数说明 http://www.sgi.com/tech/stl/Map.html
Actually RGBA color have no some default quasi-order like any scalar. And you shouldn't define one in global context, but you can define your own ordering and specify it in std::map template instance. See parameters description at http://www.sgi.com/tech/stl/Map.html