Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:12:59+00:00 2026-06-11T11:12:59+00:00

I have a templated class which looks as follows (assume a using namespace std

  • 0

I have a templated class which looks as follows (assume a using namespace std for brevity):

template <typename Type, typename Compare = less<Type>>
struct weighted_base
{
    typedef typename set<pair<Type, double>, set_compare<Type, Compare>> mapped_type;
    map<Type, mapped_type, Compare> backing_map;
    ...
};

where set_compare is defined as:

template <typename Type, typename Compare>
struct set_compare
{
     bool operator()(const pair<Type, double>& a, 
                     const pair<Type, double>& b)
     {
         return Compare(a.first, b.first);
     }
};

That is, the map takes keys of type Type to std::set<std::pair<Type, double>> values. This has some problems when I use a method such as:

void insert(const Type& from, const Type& to, double weight)
{
    //...
    mapped_type& adj_nodes = backing_map[from];
    adj_nodes.insert(make_pair(to, weight));
} 

The problem is that within the set, when it goes to call set_compare, it has a type of const Type&, not Type. Thus, assuming it’s std::less, in this case it will be trying to call less<int>::less(const int& a, const int& b) which fails.

Is there some way of fixing it so that both contains can use (effectively) the same comparison function here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:13:01+00:00Added an answer on June 11, 2026 at 11:13 am

    Compare is the type of a binary functor, so you probably need

    return Compare()(a.first, b.first);
    

    where Compare() is a temporary, defualt constructed, Compare instance. For example, if you substitute Compare for std::less<int>,

    std::cout << std::boolalpha;
    std::cout << std::less<int>(5,50) << "\n"; // Error!
    std::cout << std::less<int>()(5,50) << "\n"; // OK
    

    Your error is likely to give you many compiler errors, some of which could send you down the wrong track. I suggest fixing it first, then seeing if the code behaves as you expect it to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which looks like this: template <class In, class In2>
I have a templated container class that looks something like this: template <class ItemType>
Suppose I have an autolocker class which looks something like this: template <T> class
I have my templated container class that looks like this: template< class KeyType, class
I have a template class which looks like the following: template <template <class TypeT>
I have a templated class Rect with conversion constructor which allows conversion between Rect
I have the following templated function... template< class T > T *create_object( lua_State *L
Basically I have code which looks like this inside a header file: class Bar;
I have a (free) function template that looks like this template <typename T> T
I have a class which looks something like this. I'd prefer to have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.