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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:30:32+00:00 2026-05-15T19:30:32+00:00

Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST

  • 0

Compiling this code using g++ 4.2.1:

struct S { };
template<typename T> struct ST { };

template<typename BaseType>
class ref_count : private BaseType { };

template<typename RefCountType>
class rep_base : public RefCountType { };

class wrap_rep : public rep_base<ref_count<S> > {
  typedef rep_base<ref_count<S> > base_type;      // line 11
};

I get:

bug.cpp:1: error: ‘struct S’ is inaccessible
bug.cpp:11: error: within this context

However, if I change the wrap_rep class to use ST:

class wrap_rep : public rep_base<ref_count< ST<int> > > {
  typedef rep_base<ref_count< ST<int> > > base_type;
};

it compiles fine. Alternatively, if I change the original code to:

class wrap_rep : public rep_base<ref_count<S> > {
  typedef rep_base<ref_count< ::S > > base_type;  // now using ::
};

it also compiles fine. To me, the original code seems fine as-is. Is this a g++ bug? If not, then why does using a template work? And, for the other case, why is the ::S necessary?

  • 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-15T19:30:33+00:00Added an answer on May 15, 2026 at 7:30 pm

    Both of those codes are invalid (only the last one is valid), but your compiler (which is not conforming) only diagnoses one. As another answer says, this uses the injected class name. A class S is considered to have a member name S denoting that same class. For example (notice the “class” keyword before S::S in the first example is necessary to force a reference to the injected class name, instead of the default constructor):

    class S { };
    
    class S::S object; // creates an S object
    class X : S::S::S::S { }; // derives from class S
    

    Class templates also have an injected class name. Like the injected class name, it is inherited to derived classes, and thus ST<int> is ill-formed because it uses that injected class name, which however is not accessible. If you use GCC less 4.5, it may have something to do with a change introduced with GCC4.5:

    G++ now implements DR 176. Previously G++ did not support using the injected-class-name of a template base class as a type name, and lookup of the name found the declaration of the template in the enclosing scope. Now lookup of the name finds the injected-class-name, which can be used either as a type or as a template, depending on whether or not the name is followed by a template argument list. As a result of this change, some code that was previously accepted may be ill-formed because

    1. The injected-class-name is not accessible because it’s from a private base, or
    2. The injected-class-name cannot be used as an argument for a template template parameter.

    In either of these cases, the code can be fixed by adding a nested-name-specifier to explicitly name the template. The first can be worked around with -fno-access-control; the second is only rejected with -pedantic.


    To have a bit more fun with injected class names – notice that the injected class name is not equivalent to a typedef as one might think first. The injected class name is a class-name, but is not classified as a typedef-name, which means it can be hidden by function, object or enumerator names:

    // valid, the data-member hides the injected class name
    struct S { int S; };
    

    To refer to the injected class name you can say class S::S (likewise, in a base-class list, non-type names are ignored thus you don’t need special pre-cautions there), but a simple lookup to S::S will refer to the data-member.

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

Sidebar

Related Questions

This code is not compiling in my system; I'm using Eclipse. // Linked list
When compiling (Microsoft Visual C++ 2005 Express) this piece of code: struct A {
I have this C++ code: #include <iostream> using namespace std; struct MyItem { int
Compiling this code using Emscripten: #include <stdio.h> int main() { unsigned long d1 =
Compiling this code with g++ 4.7.0 ( -Wall -Wextra -Werror -Wconversion -std=c++11 ): #include
I am compiling following code and getting following error. How to fix this? Thanks
This morning in my CS lab, I was compiling and executing some C code.
Here is the test code template <class T> void f() { T t; t.f<T>(0);
I'm compiling using Code::Blocks on Windows 7 using the MinGW compiler (which I can
Consider this simplest piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text;

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.