This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Confusion with template and heritage


Hi,

I wanted to know if the code below is legal or not (before reporting it at a bug if it is).

8<----------------------------------------
      1 #include <iostream>
      2 #include <string>
      3 using namespace std;
      4
      5 template <class T>
      6 class A {
      7 public:
      8   void foo(int, int) {}
      9 };
     10
     11 struct B1 {
     12   void foo(int,int) {}
     13 };
     14
     15 class B2: public A<int> {
     16 public:
     17   void foo(int,int) {}
     18 };
     19
     20 template <class T>
     21 class C: public T {
     22 public:
     23   void foo(string&, string&) {}
     24 };
     25
     26 int main() {
     27   C<A<int> > ca;
     28   C<B1> cb1;
     29   C<B2> cb2;
     30   ca.foo(3,3);
     31   cb1.foo(3,3);
     32   cb2.foo(3,3);
     33 }
8<----------------------------------------

Test with g++ 3.3:

8<----------------------------
In function `int main()':
30: error: no matching function for call to `C<A<int> >::foo(int, int)'
23: error: candidates are: void C<T>::foo(std::string&, std::string&) [with T = A<int>]
31: error: no matching function for call to `C<B1>::foo(int, int)'
23: error: candidates are: void C<T>::foo(std::string&, std::string&) [with T = B1]
32: error: no matching function for call to `C<B2>::foo(int, int)'
23: error: candidates are: void C<T>::foo(std::string&, std::string&) [with T = B2]
8<----------------------------


Same results with Sun CC 5.3 for example.

8<----------------------------
line 30: Error: Formal argument 1 of type std::basic_string<char, std::char_traits<char>, std::allocator<char>>& in call to C<A<int>>::foo(std::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::basic_string<char, std::char_traits<char>, std::allocator<char>>&) is being passed int.
line 30: Error: Formal argument 2 of type std::basic_string<char, std::char_traits<char>, std::allocator<char>>& in call to C<A<int>>::foo(std::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::basic_string<char, std::char_traits<char>, std::allocator<char>>&) is being passed int.
(*SNIP*)
6 Error(s) detected.
8<----------------------------


Adding a 'using T::foo;' in class C<T> is a workaround, at least for g++ 3.3 and CC 5.3 (MSVC++ 7.0 still complaining).

Is it an expected behaviour?

--
Benoît Sibaud


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]