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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:49:52+00:00 2026-05-25T17:49:52+00:00

sometimes (quite rarely) I need to get protected members from existing variables like this:

  • 0

sometimes (quite rarely) I need to get protected members from existing variables
like this:

struct S {
protected:
 int i;
};

struct T : S {
 using S::i;
};

int main() {
 S s;
 static_cast<T&>(s).i = 0;
}

I’m almost sure this ( static_cast(s) ) is UB, but is someone know what the C++ standard (2003) says about this situation?

  • 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-25T17:49:53+00:00Added an answer on May 25, 2026 at 5:49 pm

    This type of operation is actually the basis for implementing the constant reoccurring template pattern, where inside the base-class you actually static_cast the this pointer of the base-class to the derived-class template type. Since S is an unambiguous base class of T, and you are not accessing any members from the static_cast that are not already members of S, I don’t see why you would encounter any issues.

    Section 5.2.8 on static casting in paragraph 5 states:

    An lvalue of type “cv1 B”, where B is a class type, can be cast to type “reference to cv2 D”, where D is a class derived (clause 10) from B, if a valid standard conversion from “pointer to D” to “pointer to B” exists (4.10), cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and B is not a virtual base class of D. The result is an lvalue of type “cv2 D.” If the lvalue of type “cv1 B” is actually a sub-object of an object of type D, the lvalue refers to the enclosing object of type D. Otherwise, the result of the cast is undefined.

    You seem to be meeting all the requirements that avoid undefined behavior. That is:

    1. Class T is derived from S
    2. A pointer conversion from S to T does exist since S is both accessible and an unambiguous base-class of T (requirements from 4.10)
    3. You are using the same constant-value-qualification for both types
    4. S is not a virtual base-class of T
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes it is useful to get the type name from static invoke, see this
Cross-browser handling is quite a hassle sometimes. In this case I need to capture
Sometimes I see something like this which I quite don't understand: // ... that.constructor
Sometimes normal FTP doesn't quite cut it... When you need to do secure FTP
We have a web application which sometimes (quite rarely, several times a day) gives
Quite frequently, I'm using custom Wicket components to render model objects. Objects can sometimes
Sometimes I seem to be quite inefficient. When I was nearly finished typing this,
I have been using metaprogramming quite a lot, but sometimes the combination of c
Going backwards from SQL to LINQ2SQL is sometimes quite simple. The following statement SELECT
I have a database with many columns and sometimes I need to select quite

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.