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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:16:48+00:00 2026-05-25T15:16:48+00:00

In C++0x, you can use the using keyword to inherit constructors, like so: class

  • 0

In C++0x, you can use the using keyword to inherit constructors, like so:

class B { B(int) {} };

class A : public B { using B::B; };

Which will implicitly declare an A(int) constructor. Does this work with templates?

class B { B(int) {} };

template<class T> class A : public T { using T::T; };

Within T::T, I expect the compiler to figure out the left hand T since using the scope operator on template arguments is normal, but figuring out that the right hand T is the constructor is a special case. In fact it appears there’s an ambiguity: what if I have a method called T in B that I’m trying to add overloads to in A (that’s how a compiler would interpret such a using declaration pre-C++0x)?

  • 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-25T15:16:49+00:00Added an answer on May 25, 2026 at 3:16 pm

    Yes it works, and the reason is the name lookup mechanism. The mechanism a inheriting-constructors declaration works is simple: If the using declaration’s name refers to base class constructors, that’s an inheriting constructors declaration. At 3.4.3.1[class.qual]p2 we find:

    In a lookup in which the constructor is an acceptable lookup result and the nested-name-specifier nominates a class C

    • if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name of C (Clause 9), or
    • in a using-declaration (7.3.3) that is a member-declaration, if the name specified after the nested-name-specifier is the same as the identifier or the simple-template-id’s template-name in the last component of the nested-name-specifier

    the name is instead considered to name the constructor of class C.

    This is the paragraph that makes out of class constructor definitions work, and this is also the paragraph that makes inheriting constructor declarations work. The second bullet applies in this case:

    struct B {
      B(int) { }
    };
    
    typedef B mytype;
    
    struct A : B {
      // "name ... is the same as the identifier ... in the last component ..."
      using mytype::mytype;
    };
    
    
    template<typename T> using same = T;
    
    struct C : B {
      // "name ... is the same as the template-name ... in the last component ..."
      same<B>::same;
    };
    

    The latter example proves also useful in cases such as the following

    template<template<typename> class Base>
    struct X : Base<int> {
      using Base<int>::Base;
    };
    

    In summary:

    • The first bullet above is a semantic rule – if the name after the nested name specifier refers to the injected class name (B::B or mytype::B), then it will be translated to refer to the constructor(s).

    • The second bullet is a syntactic rule – the names just must match – their meaning is irrelevant otherwise – there could have been a member called Base in the template argument provided to X, such as in the following, but the using declaration would still import the constructors and do not name the member Base:

      template<typename T> struct D { private: T Base; };
      X<D> x; // valid, the private member is *not* touched!
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you use 2 'using' statements like: using (SqlConnection ..) { using(SqlDataReader reader =
Using EF we can use LINQ to read data which is rather simple (especially
When we define routes in routes.rb using the name like map.some_link .We can use
In MySQL, you can use the keyword USING in a join when you join
PHP classes can use the keyword self in a static context, like this: <?php
Possible Duplicate: Why shall I use the “using” keyword to access my base class
How can use Python2.5 with to write scripts in vim? I'm using vim 7.2
Why I can use setcookie without any preparation while need a session_start() before using
Using PyObjC , you can use Python to write Cocoa applications for OS X.
I am wondering if you can use Linq to SQL using a .mdf file

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.