[英]Using implicit comparator with a map that has a struct as key in C++
I want to use a map of the standard libc++. 我想使用标准libc ++的映射。 So defining operator< for the key type is one solution to instante a map. 因此,为键类型定义operator <是实例化映射的一种解决方案。 I did it but it does not work. 我做到了,但是没有用。
I checked that operator< works (compilation and execution). 我检查了operator <是否有效(编译和执行)。
png_pixel p1, p2;
p1.red = 5; p2.red = 20; // init
if(p1 < p2)
puts("true");
else
puts("false");
That is the definition of operator<: 那就是operator <的定义:
#include <tuple> // C++11
bool
operator<(const png_pixel& a, const png_pixel& b)
{
return std::tie(a.red, a.green, a.blue, a.alpha) < std::tie(b.red, b.green, b.blue, b.alpha);
}
png_pixel is a struct defined like this: png_pixel是这样定义的结构:
// My source code code
typedef png::rgba_pixel png_pixel;
// libpng++ rgba_pixel.hpp http://www.nongnu.org/pngpp/
namespace png
{
template< typename T >
struct basic_rgba_pixel
{
typedef pixel_traits< basic_rgba_pixel< T > > traits;
basic_rgba_pixel()
: red(0), green(0), blue(0), alpha(0)
{
}
/**
* \brief Constructs rgba_pixel object from \a red, \a green,
* \a blue and \a alpha components passed as parameters.
* Alpha defaults to full opacity.
*/
basic_rgba_pixel(T red, T green, T blue,
T alpha = traits::get_alpha_filler())
: red(red), green(green), blue(blue), alpha(alpha)
{
}
T red;
T green;
T blue;
T alpha;
};
typedef basic_rgba_pixel< byte > rgba_pixel;
// other things
}
But when I try to use png_pixel as a key in a C++ map, GCC and Clang are not happy. 但是,当我尝试在C ++映射中使用png_pixel作为键时,GCC和Clang并不满意。
# GCC 4.9
error: no match for ‘operator<’
(operand types are ‘const png::basic_rgba_pixel<unsigned char>’
and ‘const png::basic_rgba_pixel<unsigned char>’)
# Clang 3.5
error: invalid operands to binary expression
('const png::basic_rgba_pixel<unsigned char>'
and 'const png::basic_rgba_pixel<unsigned char>')
If needed, the full source code is available in branch map-converter of https://gitlab.com/RyDroid/Libre2DCarRacingsSimulator libpng++ is packaged at least in Debian as libpng++-dev. 如果需要,完整的源代码可在https://gitlab.com/RyDroid/Libre2DCarRacingsSimulator的分支映射转换器中找到。libpng ++至少在Debian中打包为libpng ++-dev。
Regards. 问候。
Your operator<
isn't in the template definition context of std::less
, the default comparator of std::map
, and in any event is likely hidden by the gazillion operator<
s in std
for ordinary unqualified lookup, so if it is to be used, it must be found by ADL. 您的operator<
不在std::less
的模板定义上下文中,它是std::map
的默认比较器,无论如何,对于普通的不合格查找, std
的gazillion operator<
s都可能隐藏了它,因此要使用,必须由ADL找到。 But it's not in the png
namespace, the only associated namespace of png::basic_rgba_pixel<unsigned char>
, so ADL doesn't find it either. 但是它不在png
名称空间中, png::basic_rgba_pixel<unsigned char>
是png::basic_rgba_pixel<unsigned char>
的唯一关联名称空间,因此ADL也找不到它。
Write a comparator function object and pass that to map
. 编写一个比较器函数对象,并将其传递给map
。 You can also try putting it into the png
namespace, but that feels more icky. 您也可以尝试将其放入png
命名空间,但这感觉更棘手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.