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 7490619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:41:12+00:00 2026-05-29T15:41:12+00:00

I have a map of pointer to member declared as : std::map<char, T (Operand::*)(const

  • 0

I have a map of pointer to member declared as :

std::map<char, T (Operand::*)(const T &, const T &)> op_map;

I fill my map with pointer to member directly in the constructor of my class with :

op_map['+'] = &Operand::op_add;

For example, op_add source code is :

  T op_add(const T & a, const T & b) {
    return a + b;
  }

And I want to call my pointer to member from a const function. Here is the source code :

  IOperand *res_int32(char op, const IOperand & rhs) const {
    IOperand *res = const_cast<IOperand *>(&rhs);
    Operand<int> *tmp = dynamic_cast<Operand<int>*>(res);
    T res_calc = (this->*op_map[op])(_value, (T)tmp->getValue());
  }

But it makes me always an error :

Operand.hpp:70:64: error: passing ‘const std::map<char, double (Operand<double>::*)(const double&, const double&), std::less<char>, std::allocator<std::pair<const char, double (Operand<double>::*)(const double&, const double&)> > >’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char, _Tp = double (Operand<double>::*)(const double&, const double&), _Compare = std::less<char>, _Alloc = std::allocator<std::pair<const char, double (Operand<double>::*)(const double&, const double&)> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = double (Operand<double>::*)(const double&, const double&), std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]’ discards qualifiers [-fpermissive]
Operand.hpp:70:64: error: invalid conversion from ‘const Operand<double>* const’ to ‘Operand<double>*’ [-fpermissive]

Have you got any solution ?

Thank you.

  • 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-05-29T15:41:13+00:00Added an answer on May 29, 2026 at 3:41 pm

    operator[] can’t be applied to a const map, since it inserts a new element if the key is not found.

    In C++11, there is an at function which throws an exception if the key is not found:

    T res_calc = (this->*op_map.at(op))(_value, (T)tmp->getValue());
                               ^^^^^^^
    

    In C++03, you’ll need to use find:

    map_type::const_iterator found = op_map.find(op);
    if (found != op_map.end()) {
        T res_calc = (this->*(found->second))(_value, (T)tmp->getValue());
    } else {
        // handle error
    }
    

    You’ll also need to change the type of the member functions in the map to

    T (Operand::*)(const T &, const T &) const
                                         ^^^^^
    

    in order to call them on this from a const member function.

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

Sidebar

Related Questions

I have a class which contains a static member, a map of strings to
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
I have a pointer to a map that I am trying to delete (this
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have a map declared as follows: map < string , list < string
What I wish to do is have a class that contains a map of
So I have such function: boost::shared_ptr<my_class> get_class_by_name(std::string name) { typedef std::map<boost::shared_ptr<my_class>, my_description> map_t; BOOST_FOREACH(map_t::value_type
I have something like this: #include <iostream> #include <map> int main() { std::map<int, int*>
I have a class Test with a peculiar data structure. A member of class
I have a map in a multithreaded app mapping a class called uuid to

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.