Why does GCC allow inheriting from a private nested class?
Asked Answered
C

2

10

Consider the following code:

class A {
  class B {};
};

template <typename C>
class D : A::B {};

void f() {
  D<int> d;
}

D<int> inherits from A::B which is a private nested class. I was expecting this to be an error, but GCC accepts this code. Is it a bug in GCC or am I missing something?

Candie answered 11/12, 2012 at 23:14 Comment(2)
Seems like a bug, since removing the template declaration results in the expected error.Phosphene
Visual Studio 2012 dont want to compile that code.Philous
C
7

I've found the answer. Since it's might be useful for others I am posting it here - this is GCC bug 47346.

Candie answered 11/12, 2012 at 23:35 Comment(2)
@JesseGood: Sorry for that, I'm not usually answering my own question, but since there was little activity and I've found the answer. ;-)Candie
It wasn't a complaint, you posted first, so you deserve the upvotes. Also, answering your own question is accepted practice.Triphthong
P
2

Did you try to create non template derivering class?

Template class is not compiled if there is no object of that class. Try to create instance of this class or create non-template derived class - gcc will probably fail ;-)

Edit My bad - the object is created and it's not causing gcc error. Sorry for that.

Philous answered 11/12, 2012 at 23:18 Comment(1)
Ok, right. I check this, it works :| I dont understand that, really :)Philous

© 2022 - 2024 — McMap. All rights reserved.