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

  • Home
  • SEARCH
  • 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 1068025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:14:03+00:00 2026-05-16T20:14:03+00:00

Could someone explain me why this code: class safe_bool_base { //13 protected: typedef void

  • 0

Could someone explain me why this code:

class safe_bool_base
{ //13
    protected:

        typedef void (safe_bool_base::*bool_type)() const;

        void this_type_does_not_support_comparisons() const {} //18

        safe_bool_base() {}
        safe_bool_base(const safe_bool_base&) {}
        safe_bool_base& operator=(const safe_bool_base&) { return *this; }
        ~safe_bool_base() {}
};

template <typename T=void> class safe_bool : public safe_bool_base
{
    public:

        operator bool_type() const
        {
            return (static_cast<const T*>(this))->boolean_test() ? &safe_bool_base::this_type_does_not_support_comparisons : 0;
        }

    protected:

        ~safe_bool() {}
};

template <> class safe_bool<void> : public safe_bool_base
{
    public:

        operator bool_type() const
        {
            return (boolean_test() == true) ? &safe_bool_base::this_type_does_not_support_comparisons : 0; //46
        }

    protected:

        virtual bool boolean_test() const = 0;
        virtual ~safe_bool() {}
};

Produces the following compiler error ?

c:\project\include\safe_bool.hpp(46) : error C2248: 'safe_bool_base::this_type_does_not_support_comparisons' : cannot access protected member declared in class 'safe_bool_base'
c:\project\include\safe_bool.hpp(18) : see declaration of 'safe_bool_base::this_type_does_not_support_comparisons'
c:\project\include\safe_bool.hpp(13) : see declaration of 'safe_bool_base'

Since both safe_bool templates derive from safe_bool_base, I don’t understand why one can’t access a protected member of the base class.

Am I missing something ?

  • 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-16T20:14:04+00:00Added an answer on May 16, 2026 at 8:14 pm

    This should probably help (reproducible in a non template situation also)

    struct A{
    protected:
        void f(){}
    };
    
    struct B : A{
        void g(){&A::f;}        // error, due to Standard rule quoted below
    };
    
    int main(){
    }
    

    VS gives “‘A::f’ : cannot access
    protected member declared in class
    ‘A'”

    For the same code, Comeau gives

    “ComeauTest.c”, line 7: error:
    protected function “A::f” (declared at
    line 3) is
    not accessible through a “A” pointer or object void g(){&A::f;}
    ^

    “ComeauTest.c”, line 7: warning:
    expression has no effect void
    g(){&A::f;}

    Here is the fixed code which achieves the desired intentions

    struct A{
    protected:
        void f(){}
    };
    
    struct B : A{
        void g(){&B::f;}        // works now
    };
    
    int main(){
    }
    

    So, why does the first code snippet not work?

    This is because of the following rule in the C++ Standard03

    11.5/1- “When a friend or a member function of a derived class references
    a protected nonstatic member function
    or protected nonstatic data member of
    a base class, an access check applies
    in addition to those described earlier
    in clause 11.102) Except when forming
    a pointer to member (5.3.1), the
    access must be through a pointer to,
    reference to, or object of the
    derived class itself (or any class
    derived from that class) (5.2.5). If
    the access is to form a pointer to
    member, the nested-name-specifier
    shall name the derived class (or any
    class derived from that class).

    So change the return within operator functions as follows

    return (boolean_test() == true) ? &safe_bool<void>::this_type_does_not_support_comparisons : 0; //46 
    
    return (static_cast<const T*>(this))->boolean_test() ? &typename safe_bool<T>::this_type_does_not_support_comparisons : 0; 
    

    EDIT 2: Please ignore my explanations. David is right. Here is what it boils down to.

    struct A{
    protected:
        int x;
    };
    
    struct B : A{
        void f();
    };
    
    struct C : B{};
    
    struct D: A{            // not from 'C'
    };
    
    void B::f(){
        x = 2;         // it's own 'A' subobjects 'x'. Well-formed
    
        B b;
        b.x = 2;       // access through B, well-formed
    
        C c;
        c.x = 2;       // access in 'B' using 'C' which is derived from 'B', well-formed.
    
        D d;
        d.x = 2;       // ill-formed. 'B' and 'D' unrelated even though 'A' is a common base
    }
    
    int main(){} 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

could someone please explain this java code for me. i find it really hard
Could someone explain me why this piece of code randomly leaks LDAP connections? I
Could someone please explain the behavior of this class testCompile { /* * Sample
template <typename E> class VecExpression{ public: operator E&(){ return static_cast<E&>(*this); } operator E const&()
Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo
I was kind of shocked by this. Could someone explain why this works? A
I ran across this and was wondering if someone could explain why this works
I saw this tip in another question and was wondering if someone could explain
This is my test code: class PassingInActionStatement { static void Main(string[] args) { var
Can someone explain why this code: import sys sys.path.append(C:\\WINDOWS\\system32) import clr clr.AddReferenceToFile(wiimotelib.dll) works fine

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.