10

What is the actual effect of the following construct:

class Base { /* ... */ };

template<class T>
class Derived : public T { /* ... */ };

int main() {
    Derived<const Base> d;
    // ...
}

Does the Derived class only have access to the const-part of the interface of Base? My first tests indicate that there's actually no effect at all. Why?

Thanks!

phlipsy
  • 2,899
  • 1
  • 21
  • 37
  • I think, that the answer is here: http://stackoverflow.com/a/13435319/945183 – jacek.ciach Mar 24 '13 at 00:49
  • I think these questions refer to different things: The linked ones are about template *declarations* of the form `template struct test;` and my question is about template *instantiations* whose applied parameters are `const` types. – phlipsy Mar 24 '13 at 09:48

1 Answers1

2

My guess is that the const is ignored, because if you try to write

class Derived : public const Base

the program doesn't compile.

EDIT:

frozenkoi gave the relevant part of the standard in the comments:

"A typedef-name (7.1.3) that names a class type, or a cv-qualified version thereof, is also a class-name. If a typedef-name that names a cv-qualified class type is used where a class-name is required, the cv-qualifiers are ignored. A typedef-name shall not be used as the identifier in a class-head." §9.1

Community
  • 1
  • 1
alestanis
  • 21,519
  • 4
  • 48
  • 67
  • You're right, you can't write it explicitly. I guess it's like `void func() { return void(); }` - writing it explicitly it's completely useless but in the context of templates important as a corner case. – phlipsy Mar 23 '13 at 09:16
  • I think it has more to do with `const` being ignored when declaring the type than being ignored in the class declaration. (no c++ standard at hand) but it's along these lines: "The top-level cv-qualifiers on the template-parameter are ignored when determining its type" §14 – frozenkoi Mar 23 '13 at 10:41
  • 1
    I’m not convinced because that’s just a syntax issue; this compiles: http://ideone.com/ik41t3 – Konrad Rudolph Mar 23 '13 at 11:15
  • @KonradRudolph note that your sample goes through a typedef, and I think that ends up ignoring the `const` too. – frozenkoi Mar 23 '13 at 22:08
  • 4
    I think in that case the relevant part in the spec is "A typedef-name (7.1.3) that names a class type, or a cv-qualified version thereof, is also a class-name. If a typedef-name that names a cv-qualified class type is used where a class-name is required, the cv-qualifiers are ignored. A typedef-name shall not be used as the identifier in a class-head." §9.1 (I have a draft version with me, might be innacurate) – frozenkoi Mar 23 '13 at 22:18
  • 1
    @frozenkoi A typedef in general definitely does *not* ignore the `const`, as is easily verifiable by declaring a variable using the typedef, and attempting to modify it. But I think your standard quote is actually relevant here, put it in an answer. – Konrad Rudolph Mar 23 '13 at 23:41