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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:15:42+00:00 2026-06-07T07:15:42+00:00

I came across some code like this: struct A { A() {} A(int) {}

  • 0

I came across some code like this:

struct A {
    A() {}
    A(int) {}
};

struct B : A {
    void init(int i);
};

void B::init(int i) {
    A::A(i); // what is this?
}

int main() {
    B b;
    b.init(2);
}

This compiled and ran using VC11 beta with no errors or warnings with /W4.

The apparent intent is for calling B::init to reinitialize the B’s A base subobject. I believe it actually parses as a variable declaration for a new variable named i with type A. Compiling with clang produces diagnostics:

ConsoleApplication1.cpp:11:14: warning: declaration shadows a local variable
        A::A(i);
             ^
ConsoleApplication1.cpp:10:22: note: previous declaration is here
    void B::init(int i) {
                     ^
ConsoleApplication1.cpp:11:14: error: redefinition of 'i' with a different type
        A::A(i);
             ^
ConsoleApplication1.cpp:10:22: note: previous definition is here
    void B::init(int i) {
                     ^

It seems curious that the type can be referred to with the redundant class qualification.

Also, A::A(i) appears to be parsed differently by VS11 and clang/gcc. If I do A::A(b) clang and gcc create a variable b of type A using the default constructor. VS11 errors out on that saying b is an unknown identifier. VS11 appears to parse A::A(i) as the creation of a temporary A using the constructor A::A(int) with i as the parameter. When the redundant qualifier is eliminated VS parses the source as a variable declaration like clang and gcc do, and produces a similar error about shadowing the variable i.

This difference in parsing explains why VS11 will choke on more than a single extra qualifier; A::A::A::A(i), and why, given that clang and gcc can accept one extra qualifier, any number more than one extra has the same result as one extra.

Here’s another example with the redundant qualifiers in a different context. All compiler seem to parse this as a temporary construction:

class Foo {};

void bar(Foo const &) {}

int main() {
    bar(Foo::Foo());
}
  1. Why are redundant qualifiers allowed at all?
  2. There are some contexts where constructors can be referred to, such as the syntax for inheriting constructors (class D : B { using B::B; };) but VS seems to be allowing it anywhere. Is VS wrong and are clang and gcc right in how redundant qualifiers are parsed?
  3. I know VS is still a fair bit behind in terms of standards compliance, but I do find it a bit surprising that modern, actively developed compilers could be so divergent, in this case resolving a redundant qualifier as the name of a constructor (even though constructors don’t have names) vs. resolving redundant qualifiers simply to the type, resulting in VS constructing a temporary where the others declare a variable. It can be made even worse where B b(A::A(i)); is parsed by clang and gcc as the most vexing parse, but VS sees it as declaring a variable b of type B with an initializer. Are there still many differences this severe?
  4. Clearly, redundant qualifiers should be avoided in portable code. Is there a good way to prevent this construct from being used?
  • 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-06-07T07:15:44+00:00Added an answer on June 7, 2026 at 7:15 am

    While the phenomenon can probably be attributed to class name injection, as noted in ephemient’s answer, for this specific example it has been outlawed by C++ language quite a while ago.

    http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#147

    The combination A::A is required to refer to class constructor, not to the class injected name. The A::A(i) is supposed to be interpreted by a compliant compiler as an illegal (and therefore meaningless) expression involving constructor name. Comeau compiler, for one example, will refuse to compile your code for that reason.

    Apparently VC11 continues to treat A::A as a reference to the injected class name. Interestingly enough, I don’t observe this problem in VS2005.

    Back in the day when A::A was interpreted as referring to the injected name, one could declare an A object as

    A::A::A::A::A::A a;
    

    and so on, with arbitrary number of As. But not anymore. Surprisingly, version of GCC (4.3.4?) used by ideone still suffers from this issue

    http://ideone.com/OkR0F

    You can try this with your version of VC11 and see if it allows that.

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

Sidebar

Related Questions

While browsing some source code I came across a function like this: void someFunction(char
If you came across some C# code like this with nested using statements/resources: using
Context I came across some code, like this: if( Some_Condition ) throw 0; I
I came across some legacy code that contains a function like this: LPCTSTR returnString()
I came across some tests that included code like this: if (this != null)
I recently came across some code like this: parent && parent.removeChild(this); Which takes the
I came across some javascript code like this: selector = $this.attr('href') selector = selector
I came across some code today that looks like this: subroutine sub(hello,world,this,routine,takes,a, & &
I recently came across some code that looks something like this: <head> <?php /*
I came across a jQuery snippet like this: selector.eventHandler(...some code...).eventHandler(...some code...); What does it

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.