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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:04:05+00:00 2026-05-28T08:04:05+00:00

The following code fails to compile on g++ 4.6.1: template<class Base> struct GetBase {

  • 0

The following code fails to compile on g++ 4.6.1:

template<class Base>
struct GetBase {
  Base * getBase() {
    return static_cast<Base *>(this);
  }
};

template<class Derived>
struct Parent : private GetBase<Derived> {
  using GetBase<Derived>::getBase;
  int y() {
    return getBase()->x();
  }
};

struct Child : public Parent<Child> {
  int x() {
    return 5;
  }
  int z() {
    return y();
  }
};

with the error

In member function ‘Base* GetBase<Base>::getBase() [with Base = Child]’:
    instantiated from ‘int Parent<Derived>::y() [with Derived = Child]’
    instantiated from here
error: ‘GetBase<Child>’ is an inaccessible base of ‘Child’

Changing the static_cast to a reinterpret_cast will get the code to compile and it will work in this case, but I’m wondering if this is an acceptable solution in all cases? i.e., is it ever the case that a pointer to a base class is not the same as this? I’m assuming with multiple inheritance this may happen if the parents have data members? If GetBase is the first superclass, are the this pointers guaranteed to be equal?

  • 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-28T08:04:05+00:00Added an answer on May 28, 2026 at 8:04 am

    A good question; it caused me to learn something new about static_cast.

    I think the following code achieves what you want : provides a base class for CRTP with a member function that casts this to the Derived type, yet only makes it accessible to the direct descendants of this base class. It compiles with GCC 4.3.4 (tested at ideone) and Clang (tested at llvm.org). Sorry, I couldn’t resist to changing the names that I find confusing.

    #include <iostream>
    
    template<class Derived>
    class CRTP {
    protected:
      Derived * derived_this() {
        return static_cast<Derived *>(this);
      }
    };
    
    template<class Derived>
    struct Parent : public CRTP<Derived> {
    private:
      using CRTP<Derived>::derived_this;
    public:
      int y() {
        return derived_this()->x();
      }
    };
    
    struct Child : public Parent<Child> {
      int x() {
        return 5;
      }
      int z() {
        return y();
      }
    };
    
    int main() {
      std::cout << Child().z() << std::endl;
      return 0;
    }
    

    This variant works because the inheritance is public, which enables the standard conversion of a pointer-to-derived-class to a pointer-to-base-class, and so also the inverse conversion (from base to derived) with static_cast, which CRTP needs. Private inheritance in your code prohibited this. So I made inheritance public, changed the method to be protected, and in Parent further restricted access by putting the using declaration into private section.

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

Sidebar

Related Questions

Please explain why the following code snippet fails to compile: public class ScjpTest{ static
I have the following code and it fails to compile template < typename T
The following code fails to compile stating A local variable named 'st' cannot be
The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace
The following code fails in 'Evaluate' with: This expression was expected to have type
I am attempting to compile the following template based code in VC++ 2005. #include
The following code template<typename T, typename U> class Alpha { public: template<typename V> void
This code fails to compile using g++ 4.2.1 but works fine under vc++ v8.
The following Scala code fails to compile in Scala 2.7.7, with a type mismatch
The following code fails to compile with message: cannot instantiate type SymmetricKey SymmetricKey is

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.