Operator Disambiguation

I have the following type of card ...

std::map<D3DXCOLOR, ID3DXMesh*>

At compile time, xfunctional complains that it cannot resolve the ambiguity regarding the type of key;

error C2593: 'operator <' is ambiguous

Candidate operators detected by the compiler are as follows:

  • built-in operator C ++ <(DWORD, DWORD)
  • built-in operator C ++ <(FLOAT, FLOAT)
  • C ++ built-in operator <(D3DCOLORVALUE, D3DCOLORVALUE)

The D3DXCOLOR structure consists of 4 floats r , g , b and a , but do not define the operator <. However, it does provide throw functions for DWORD FLOAT and D3DCOLORVALUE, therefore, entries in the candidate list.

I am considering the best way to solve this problem. I could write my own built-in operator for D3DXCOLOR, wrap the color inside a new class that provides my own <operator, or can I somehow hint at the compiler, which implementation should I choose from the list of candidates? The DWORD <operator will satisfy my requirements adequately.

+3
source share
5 answers

You have three options. Suppose, for example, that you want them to be compared as colored values:

1) Define operator<:

bool operator<(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
    return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
}

2) Specialize std::less:

namespace std {
    template <>
    struct less<D3DXCOLOR> {
        bool operator()(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
            return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
        }
    };
}

3) - , , , , . , , "" .

struct mycomparator {
    bool operator()(const D3DXCOLOR &lhs, const D3DXCOLOR &rhs) {
        return static_cast<D3DCOLORVALUE>(lhs) < static_cast<D3DCOLORVALUE>(rhs);
    }    
};

std::map<D3DXCOLOR, ID3DXMesh*, mycomparator>
+3

map, .

struct D3DXCOLOR_less {
    bool operator ()(D3DXCOLOR const&a, D3DXCOLOR const& b) const { … }
};

std::map<D3DXCOLOR, ID3DXMesh*, D3DXCOLOR_less> foo;

, , operator < .

+2

<, .

struct CompareColor {
  bool operator()(D3DXCOLOR const & L, D3DXCOLOR const & R) const {
    // Compare and return whether L is less than R
  }
}

map<D3DXCOLOR, ID3DXMesh*, CompareColor> TheMap;
0

operator< D3DXCOLOR,

bool operator<(const D3DXCOLOR &c1, const D3DXCOLOR &c2)
{
   return <some boolean value>;
}

Or, define a compare functor called, D3DXCOLOR_LESSand pass it as the third parameter to std::map:

struct D3DXCOLOR_LESS
{
    bool operator()(const D3DXCOLOR &c1, const D3DXCOLOR &c2)
    {
       return <some boolean value>;
    }
};

std::map<D3DXCOLOR, ID3DXMesh*, D3DXCOLOR_LESS>  colormap;
0
source

In fact, RGBA color has no default quasi-order like any scalar. And you should not define it in a global context, but you can define your own order and specify it in the template instance std :: map. See Parameter Description at http://www.sgi.com/tech/stl/Map.html

0
source

All Articles