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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:37:19+00:00 2026-06-17T05:37:19+00:00

Is it safe (in theory or in practice) to reinterpret_cast a std::pair<T1, T2> const

  • 0

Is it safe (in theory or in practice) to reinterpret_cast a std::pair<T1, T2> const & into a std::pair<T1 const, T2> const &, assuming that the programmer hasn’t intentionally done something weird like specializing std::pair<T1 const, T2>?

  • 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-17T05:37:20+00:00Added an answer on June 17, 2026 at 5:37 am

    It’s NOT portable to do so.

    std::pair requirements are laid out in clause 20.3. Clause 17.5.2.3 clarifies that

    Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members. An implementation may define static or non-static class members, or both, as needed to implement the semantics of the member functions specified in Clauses 18 through 30 and Annex D.

    This implies that it’s legal (although incredibly unlikely) for an implementation to include a partial specialization such as:

    template<typename T1, typename T2>
    struct pair<T1, T2>
    {
        T1 first;
        T2 second;
    };
    
    template<typename T1, typename T2>
    struct pair<const T1, T2>
    {
        T2 second;
        const T1 first;
    };
    

    which are clearly not layout-compatible. Other variations including inclusion of additional non-static data members possibly before first and/or second are also allowed under the rule.


    Now, it is somewhat interesting to consider the case where the layout is known. Although Potatoswatter pointed out DR1334 which asserts that T and const T are not layout-compatible, the Standard provides enough guarantees to allow us to get most of the way anyway:

    template<typename T1, typename T2>
    struct mypair<T1, T2>
    {
        T1 first;
        T2 second;
    };
    
    mypair<int, double> pair1;
    mypair<int, double>* p1 = &pair1;
    int* p2 = reinterpret_cast<int*>(p1); // legal by 9.2p20
    const int* p3 = p2;
    mypair<const int, double>* p4 = reinterpret_cast<mypair<const int, double>*>(p3); // again 9.2p20
    

    However this doesn’t work on std::pair as we can’t apply 9.2p20 without knowing that first is actually the initial member, which is not specified.

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

Sidebar

Related Questions

Is this conversion type safe vector<int*> a; const vector<const int*>& b = reinterpret_cast<const vector<const
Is it safe to do the following? std::list<Something> someList; ... someList.push_back(Something(8)); Something* something =
Is the following code safe? class B { public: int& b; B (int& _b)
Is the following code safe? boost::any any_value; { std::string s = HelloWorld; any_value =
EDIT : I believe its safe to say that Flash is finally being discontinued.
After some serious googleing I found out that the RandomAccessFile-class is not thread-safe. Now
Is it safe to use the 'raw' helper in a Rails app that may
I have the feeling that I should not care about thread safe accessing /
I was reading in MSDN that a List is thread safe when used as
Possible Duplicate: Malloc thread-safe? I heard that glibc malloc() was not thread safe, since

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.