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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:58:52+00:00 2026-05-23T10:58:52+00:00

I have a class with an std::map of pointers as a member. Now, I’d

  • 0

I have a class with an std::map of pointers as a member. Now, I’d like to expose that member in a read only fashion: modification is not allowed for neither the map, nor the objects pointed to. Internally I need those pointers to be non-const, and I want to expose them as const.

I do have a solution that compiles at least, but I’d like to know if there’s any hidden problems I’ll run into with this.

class A
{
public:
  const std::map<int, const float*>& GetMap() const { return *(reinterpret_cast< const std::map<int, const float*>* >( &m_Map)); }

private:
  std::map<int, float*> m_Map;
};

There’s a possible problem I can think of: if the internal layout of std::map is different for maps of pointers and maps of const pointers, then this will cause ugly bugs. But I cannot think of any sane reason why that would be the case. Anybody has any idea?

To clarify: I am aware that this is a hack, and there are safer solutions (like separate accessor functions). I am just wondering if this would break right away because of some piece of information I’m missing.

  • 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-23T10:58:53+00:00Added an answer on May 23, 2026 at 10:58 am

    This is of course undefined (EDIT: it looks like it’s actually only unspecified) behavior because the two maps are (from the language point of view) totally unrelated types. It may appear to work now but sometime it’s going to break and cause a ton of headaches.

    Did you consider that instead of exposing an implementation detail (that you’re using map internally) you could provide const_iterators and a find method for your class’s public interface instead?

    EDIT:
    See 5.2.10/7:

    A pointer to an object can be
    explicitly converted to a pointer to
    an object of different type. 65) Except
    that converting an rvalue of type
    “pointer to T1” to the type “pointer
    to T2” (where T1 and T2 are object
    types and where the alignment
    requirements of T2 are no stricter
    than those of T1) and back to its
    original type yields the original
    pointer value, the result of such a
    pointer conversion is unspecified.

    From that quote we conclude that casting from the map with non-const value type to the map with const value type has unspecified behavior. Further, actually dereferencing the converted pointer would probably violate the strict aliasing rules and result in undefined behavior.

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

Sidebar

Related Questions

I have a template class that contains a std::map that stores pointers to T
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
I have one base class which holds a map for function pointers like this
Assume I have a class foo, and wish to use a std::map to store
I have a class that includes a std::list and wish to provide public begin()
What I wish to do is have a class that contains a map of
I have a class that requires a boost::variant containing shared pointers to various types
I have a map of pointer to member declared as : std::map<char, T (Operand::*)(const
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }

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.