jterm
jterm

Reputation: 1093

Anonymous type warning when using lambda as a comparator in map

I have a map that uses the date fields of the object to determine the maps order. To do this I use a lambda expression to handle the comparison. This works fine but I get a warning saying warning: ‘Foo’ has a field ‘Foo::m_date_map’ whose type uses the anonymous namespace The issue seems to be in the fact that I alias the type. I saw in one answer that the fix is to name the anonymous type, but I'm not sure how to do that.

//In Foo.h

static constexpr auto compare_by_date = [](const date_key* lhs, const date_key* rhs) {
    return std::tie(lhs->year, lhs->month, lhs->day) < std::tie(rhs->year, rhs->month, rhs->day);
};

class Foo {
  using ValueMap = std::map<double, date_key* const>;
  using DateMap = std::map<date_key* const, ValueMap *, decltype(compare_by_date)>;

  DateMap * m_date_map;

  Foo();

}

//In Foo.cpp
Foo::Foo() : m_date_map(new DateMap(compare_by_date) {
    // Do something
}

How should I go about getting rid of the warning?

Upvotes: 2

Views: 716

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

I think converting the lambda to a std::function would work.

static constexpr std::function<bool(const date_key*, const date_key*)> compare_by_date = ...

Upvotes: 1

Related Questions