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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:26:27+00:00 2026-05-27T08:26:27+00:00

Is the following code legal? MSVC 9 and g++ 4.4 disagree: struct base {

  • 0

Is the following code legal? MSVC 9 and g++ 4.4 disagree:

struct base
{
  struct derived {};
};

struct derived : base {};

int main()
{
  typedef derived::derived type;
  return 0;
}

MSVC complains, confusing the nested name for the type’s constructor:

c:\dev>cl test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
test.cpp(10) : error C2146: syntax error : missing ';' before identifier 'type'
test.cpp(10) : error C2761: '{ctor}' : member function redeclaration not allowed

test.cpp(10) : error C2065: 'type' : undeclared identifier

While g++ does not:

$ g++ --version test.cpp
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For context, my code contains an iterator called pointer. To provide the iterator interface, it provides the nested type pointer, which is a synonym for itself.

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

    Comeau thinks that your code is incorrect, so I think that the constructor-interpretation shadowing the type-interpretation is what the standard mandates1.

    Still, your code happily compiles if you clear up the ambiguity and tell the compiler that you’re trying to access the derived member of the base class:

    struct base
    {
      struct derived {};
    };
    
    struct derived : base {};
    
    int main()
    {
      typedef derived::base::derived type;
      return 0;
    }
    

    By the way, the fact that the constructor-interpretation prevails kinda makes sense: you have a well-known way to tell the compiler that you want to refer to stuff of the base class (via the scope resolution operator), but you wouldn’t have a syntax to do the reverse (forcing the compiler to understand that you’re referring to the constructor). So the “constructor-by-default” behavior seems quite sensible.


    1. I may look it up later, but I don’t guarantee that I actually will, this kind of pesky name issues are always a mess to look up.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following code: typedef struct { int type; } object_t; typedef struct
Is the following code legal? int add(int a, int b) { return a +
Is the following code legal? char* randomMethod1() { char* ret = "hello"; return ret;
Is the following code legal according to C99? ... for(....) { int x =
Consider the following code: void Increment(int *arr) { arr++; } int main() { int
The following code is illegal: public struct MyStruct { public MyStruct(int a, int b)
Given the following contrived example code: struct abc { int x[5]; int y[5]; };
Is the following legal C++ code: class C { static public int x; };
While working with pointers i wrote the following code, int main() { int a[]={10,20,30,40,50};
Depending on the compiler the following code: int main() { srand( 0 ); if(

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.