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

  • Home
  • SEARCH
  • 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 6709081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:50:16+00:00 2026-05-26T07:50:16+00:00

Why would some compilers insist on qualifying members public members of template base class

  • 0

Why would some compilers insist on qualifying members public members of template base class while the not requiring the same for non-template class? Please look at the following code listings:

Template class:

#include <iostream>

using namespace std;

template <class T>
class TestImpl {
public: // It wont make a difference even if we use a protected access specifier here
    size_t vval_;
    TestImpl(size_t val = 0) : vval_(val) { }
};

template <class T>
class Test : public TestImpl<T> {
public:
    Test(size_t val) : TestImpl<T>(val) {
        cout << "vval_ : " << vval_ << endl; // Error: vval_ was not declared in this scope
        //! cout << "vval_ : " << TestImpl<T>::vval_ << endl; // this works, obviously
    }
};

int main() {
    Test<int> test1(7);

    return 0;
}

Non-template class:

#include <iostream>

using namespace std;

class TestImpl {
public: // It wont make a difference even if we use a protected access specifier here
    TestImpl(size_t val = 0) : vval_(val) {}
    size_t vval_;
};

class Test : public TestImpl {
public:
    Test(size_t val) : TestImpl(val) {
        cout << "vval_ : " << vval_ << endl;
    }
};

int main() {
    Test test1(7);

    return 0;
}

The significant difference between the above code listings is that while the first listing uses template classes, the second one doesn’t.

Now, both listings will compile fine with Microsoft’s Visual Studio Compiler (cl) but the first listing WONT compile with both the Digital Mars Compiler (dmc) and Minimalist GNU for Windows (MinGW – g++) compiler. I will get an error like “vval_ was not declared in the scope” – an error I obviously understand what it means.

If I qualify access to the TestImpl‘s public variable vval_ using TestImpl<T>::vval_ the code works. In the second listing, the compilers do not complain when the derived class accesses the base class’ vval_ variable without qualifying it.

With regard to the two compilers and possibly others, my question would be why I should be able to directly access (without qualifying) vval_ variable directly from a non-template class inheriting from a non-template class, while I cant do the same from a template class inheriting from a template class?

  • 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-26T07:50:17+00:00Added an answer on May 26, 2026 at 7:50 am

    The problem you are facing is that for the compiler vval_ is not a dependent name, so it will try to look it up before the actual instantiation of the template with the type. At that point, the base type is not yet known to the compiler [*], and thus it does not consider the templated bases. Visual Studio does not perform the two phase lookup, and thus this is not required there.

    The solution is transforming the identifier into a dependent identifier, which can be done in one of multiple ways. The simplest and recommended would be using this (as in this->vval_). By adding the explicit this, the compiler knows that vval_ can differ depending on the template arguments, it is now a dependent name, and it postpones lookup to the second phase (after argument substitution).

    Alternatively, you can qualify the type that the identifier belongs to, as @mrozenau suggests, by using TestImpl<T>::vval_. Again that makes the identifier dependent on the template argument T and lookup is postponed. While both of them serve the ultimate purpose of postponing the lookup to a later time, this second approach has the extra side effect that dynamic dispatch will be disabled. In this particular case it does not matter, but if vval_ was actually a virtual function then this->f() would call the final overrider, while TestImpl<T>::f() would execute the overrider present in TestImpl<T>.

    [*] During the first phase verification of templates, before the arguments are substituted into the template, the base type is not yet known. The reason for this is that different sets of arguments might trigger the selection of different specializations of the base template.

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

Sidebar

Related Questions

The trivial solution would be: class Number { public: bool isFinite(); bool isPositive(); double
I expect it would break some other parse but this is not immediately springing
I would like to pass some (dll or not) function pointers as arguments to
Some compilers failed on non-ASCII characters in JavaDoc and source code comments. What is
I have some template code that I would prefer to have stored in a
I'm trying to compile some code which contains the following declaration, because I would
I am very new to the Xcode environment. I would like to compile some
I am working in CoffeeScript (writing a Cakefile). I would like to compile some
I would like to compile and distribute (on .net) some python programs that work
Would love some opinions on this problem I'm trying to workout. I'm trying to

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.