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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:25:58+00:00 2026-05-27T23:25:58+00:00

Possible Duplicate: Accessing inherited variable from templated parent class I have been implementing a

  • 0

Possible Duplicate:
Accessing inherited variable from templated parent class

I have been implementing a binary heap as a subclass of priority_queue, and run into a situation I cannot understand, despite considerable effort. Here is the working version of the code. (Background: c is priority_queue’s backing vector.)

#include <queue>
using namespace std;

template <class nodetype>
class BinaryHeap : public priority_queue<int> {
public:
    BinaryHeap() { vec = &c; }
    vector<nodetype> *vec;
};

int main() {
    BinaryHeap<int> b;
}

However, when you change the superclass to:

class BinaryHeap : public priority_queue<nodetype>

…the compiler complains about the usage of c:

h.cpp: In constructor ‘BinaryHeap<nodetype>::BinaryHeap()’:
h.cpp:10: error: ‘c’ was not declared in this scope

This seems all the more strange because:

  1. “[c] is equivalent to this->c” (ref) – and if you use vec = &this->c, it does indeed compile.

  2. If you add a using-declaration, using priority_queue<nodetype>::c to BinaryHeap, again, it compiles. But this using-declaration ought to be unnecessary.

Edit:

So, apparently this happens because “the compiler does not look in dependent base classes when looking up nondependent names” (ref) – “c” does not depend on a template parameter, and so is nondependent, and priority_queue<nodetype> does depend on a template parameter – nodetype – so is dependent.

priority_queue<int>, on the other hand, doesn’t depend on nodetype, and so is nondependent, and so the compiler does look in it for names when resolving other nondependent names.

Is this correct? And if so:

Why does the standard mandate this behaviour, when “[looking] in dependent base classes when looking up nondependent names” is clearly possible. What is the benefit? A compile-time performance optimisation?

  • 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-27T23:25:59+00:00Added an answer on May 27, 2026 at 11:25 pm

    In the end its simply a matter of the standard defining such a behaviour for templates. The (C++11) standard says the following about non dependent name resolution in [temp.nondep] (14.6.3) :

    Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used.

    Since c doesn’t obviously depend on a template parameter the compiler treats it as a non-dependent name. So what happens is basically the following: When the compiler looks at BinaryHeap the actual type of notetype is unknown and therefore priority_queue<nodetype> is too, since it depends on nodetype (it could be a partial specialization). Therefore the compiler can’t look into that type for the resolution (since we are talking about the point where the template is defined, not where it is instantiated). So it looks in the containing scopes for something called c, finding none and therefore rejecting the code. Using this->c (or using priority_queue<nodetype>::c) makes c a dependent name (since its not a member of BinaryHeap it must be a member of priority_queue<nodetype>), so the name lookup is delayed until the point of instantiation of the template, where notetype is known and the compiler can therefore search in priority_queue<nodetype>.

    For your edit: yes, that is correct

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

Sidebar

Related Questions

Possible Duplicate: Accessing inherited variable from templated parent class There's this class: template<typename T>
Possible Duplicate: Accessing Class Properties with Spaces i have and object file and i
Possible Duplicate: Accessing Password Protected Network Drives in Windows in C#? I have ComputerA
Possible Duplicate: Accessing scala.None from Java In Java you can create an instance of
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Accessing private members Is it possible to access private members of a

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.