4

The following code

template<int c>
struct Base
{
    static const int a = c + 5;
};

template<int c>
struct Derived : Base<c>
{
    static const int b = a + 5;
};

... fails to compile because a was not declared in this scope. Explicitly specifying Base<c>::a works, but logically this shouldn't be necessary because we're deriving from Base<c>. Is this intended behaviour (and why) or am I missing something?

hammil
  • 149
  • 6
  • @ TartanLlama although that duplicate does not explain why this works in MSVC – Shafik Yaghmour Jun 30 '15 at 20:18
  • @ShafikYaghmour from the third answer: "Actually, VC doesn't do two-phase lookup. That's why it compiles there. And that's why it is a bad idea to create a template lib using VC -- you'll have a lot of stuff to fix when you need it on any other compiler." – TartanLlama Jun 30 '15 at 20:20
  • @TartanLlama somewhat dubious to rely on comment since they are not meant to stick around but don't have time to think about it now. – Shafik Yaghmour Jun 30 '15 at 20:23

1 Answers1

-4
template<int c>
struct Derived : Base<c>
{
    static const int b = Base<c>::a + 5;
};

It really depends on compiler, but gcc requires it, it plays stupid when it comes to template classes

a o
  • 29
  • 3
  • It does not depend on a compiler. From what i've seen it will work in MSVC but this is because of extensions. Look here: http://stackoverflow.com/questions/1120833/derived-template-class-access-to-base-class-member-data – riodoro1 Jun 30 '15 at 19:59
  • You have repeated exactly the solution already mentioned in the question, but you haven't explained *why* it's necessary. Your only original contribution is to call the compiler stupid, which is not constructive. You have not answered the question. – Rob Kennedy Jun 30 '15 at 19:59
  • riodoro1 - MSVC works not because of the "extension", it just works because developers of the compiler decided it is something that should work. – a o Jun 30 '15 at 20:06
  • Rob Kennedy - I said it plays stupid, not the same thing. The exact explanation from gcc something like - it is difficult to resolve base classes members, or some bs like that . We struggle with this behavior since gcc 3.x. MSVC never had the problem , nor , I believe, does Borland – a o Jun 30 '15 at 20:10