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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:46:49+00:00 2026-06-17T08:46:49+00:00

I am trying to understand some details of alias-declared classes through C++11’s using and

  • 0

I am trying to understand some details of alias-declared classes through C++11’s using and how/why this affects a base class constructor call.

Example Code

#include <iostream>

namespace N {

  template<typename T>
  struct Foo
  {
//     Foo(){}; // NOTE: If this line is present, error 2 goes away.
    Foo(T t):value(t){};
    ~Foo() = default;
    T value; 
  };

  struct BarInt : public Foo<int>
  {
    BarInt(int t):Foo< int >(t){};
    ~BarInt() = default;
  };

  using BarFloat = Foo<float>;

};

struct A : public N::BarInt
{
  A(int i=42):BarInt(i){}; //BarInt(i) or N::BarInt(i) doesn't matter here
  ~A() = default;
};

struct B : public N::BarFloat
{
  B(float f=23.0):BarFloat(f){}; //two errors with gcc4.7.2 (with -std=gnu++11)

//   B(float f=23.1):N::BarFloat(f){}; //this line seems to work.
  ~B() = default;
};


int main(int argc, char **argv)
{
  A a;
  B b;  
  std::cout << "a's value is "<< a.value << "\n"
            << "b's value is "<< b.value << std::endl;
  return 0;
}

gcc 4.7.2 (compiling with -std=gnu++11) generates two errors for this code which I believe to be related (although I do not understand how…)

Error 1

main.cpp: In constructor ‘B::B(float)’:
main.cpp:32:19: error: class ‘B’ does not have any field named ‘BarFloat’

My searches on stackoverflow brought up Is a namespace required when referring to the base class, which mentions injected class name as a starting point for a further search. However, from what I gathered, this explains why I can write the constructor for A the way I did it (i.e. as A(int i=42):BarInt(i){};) and why BarInt(i) does not have to be qualified with the namespace N.

So why doesn’t that work with B? According to What is the difference between 'typedef' and 'using' in C++11?, using is the same as a good old typedef, so I guess my question for the first error is how alias-declared class (BarFloat in my example) differ from regular classes (BarInt in my example) in the context of injecting class names. Any pointers are greatly appreciated 🙂

Error 2

main.cpp:32:29: error: no matching function for call to ‘N::Foo<double>::Foo()’
main.cpp:32:29: note: candidates are:
main.cpp:9:5: note: N::Foo<T>::Foo(T) [with T = double]
main.cpp:9:5: note:   candidate expects 1 argument, 0 provided
main.cpp:6:10: note: constexpr N::Foo<double>::Foo(const N::Foo<double>&)
main.cpp:6:10: note:   candidate expects 1 argument, 0 provided

This error goes away if I, as already noted in the example code above, introduce an empty Foo() constructor. The question I have, however, is why BarFloat(f) triggers a call to the empty Foo() constructor and, in that case, how BarFloat.value would be possibly set to 23.0.

Post Scriptum

As this is my first post here: Hello stackoverflow and thank you all for the tremendous help you already provided to me through helping others with their problems!

  • 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-17T08:46:49+00:00Added an answer on June 17, 2026 at 8:46 am

    When you inherit from a class, the derived class can access the names of the base class (including the base class name itself) without qualification. You are effectively inheriting the names in the base class. This is why inheriting from N::BarInt allows you to refer to BarInt within A without qualification.

    For B, you are inheriting from Foo<double> using the BarFloat alias, but Foo<double> doesn’t contain BarFloat, so you don’t inherit that name.

    The second error is just because of the first failure. Since the compiler didn’t see a valid initialization of the base, it is just like you didn’t explicitly ininitialize the base at all, so it is forced to initialize it with the default constructor, which you don’t have.

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

Sidebar

Related Questions

This might be difficult to answer. I am trying to understand some codebase and
I am trying to understand some of the subtle details of python generators. One
I'm a beginner iOS/ObjectiveC coder and am trying to understand some details rather than
I am now trying to understand some code and I have found a pattern,
I'm trying to understand some MATLAB source codes. I don't know exactly which one
I have some legacy C++ code that I am trying to understand a bit
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I am trying to understand the Javascript event handling. I like some code to
I've in inherited some code that I'm trying to understand and any searching I
I'm trying to understand how ID3 tags work, so, after reading some documentation, I

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.