C ++ compiler error with CRTP

I have the following class hierarchy:

template <typename T> class base { public: void f() {} }; class class_a : public base<class_a> {}; class class_b : public base<class_b>, public class_a { using base<class_b>::f; }; int main() { class_b b; bf(); return 0; } 

Comeu and Intel C ++ v11 claim that everything is fine, however GCC (4.4.1) and VC ++ 2008 seem to complain ( http://codepad.org/KQPDsqSp ), for example:

 g++ -pedantic -Wall -o test test.cpp test.cpp: In function 'int main()': test.cpp:5: error: 'void base<T>::f() [with T = class_b]' is inaccessible test.cpp:14: error: within this context 

I believe that the code is well-formed as it is, but I could be wrong, I hope that someone from the SO C ++ community can give some idea about this problem.

Note. Adding "public" before the use directive in class_b fixes the problem for both gcc and VS. Should the scope of the class in which the using directive be applied override the derivation mode (open, closed) of the base class?

In short, it is

  • Compiler error - if so, then the GCC, VS or Comeu, Intel compiler
  • Is the code above formed correctly?
  • Is the access section called using using called to override the database derivation mode?
+6
c ++ templates compiler-errors crtp
source share
1 answer

What you are doing here is to eliminate the ambiguity by importing the character into the private namespace. Consequently, the method obscures and changes the appearance of the personal. You cannot have two functions with the same prototype, both private and public, so f is now private.

At the very least, GCC believes that use should be able to change the visibility of a function .

However, vague references found in the GCC database indicate that usage should in fact not be volume dependent.

Most importantly, the direct answer (C ++ Standard '03 - 7.3.3 / 15)

An alias created using an ad is normally accessible to a member.

Therefore, the answers would be as follows:

  • this is a mistake in comeau
  • No, the code is not very well formed, at least C ++ 03 wise (cannot find anything related to C ++ 0x N3000)
  • yes you can change the scope
+3
source share

All Articles