This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
g++ operator() inheritance problem
- From: Bill Beckett <wb at typhoon dot graysoft dot com>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sun, 23 Jun 2002 19:43:25 -0700
- Subject: g++ operator() inheritance problem
June 23, 2002
GNU g++,
I believe that you will find that the following pair of
programs demonstrate a curious problem --
#include <iostream>
/* The idea here is for B to inherit the operator()(int) from */
/* A. */
struct A
{
void operator ()(int i)
{
cout << "A.operator(), i = " << i << endl;
}
};
struct B : A
{
};
/* And so it does, The program compiles without warnings and */
/* runs correctly, printing */
/* A.operator(), i = 3 */
int main()
{
B b;
b(3);
}
Now consider --
#include <iostream>
/* However, when a different function call operator, */
/* operator()(), is added to B, the operator()(int) defined */
/* in A appears to no longer be inherited. */
struct A
{
void operator ()(int i)
{
cout << "A.operator(), i = " << i << endl;
}
};
struct B : A
{
void operator()()
{
cout << "B.operator()" << endl;
}
};
/* In this case the program will not compile. g++ -Wall produces -- */
/* t1b.cpp: In function `int main()': */
/* t1b.cpp:33: no match for call to `(B) (int)' */
/* t1b.cpp:18: candidates are: void B::operator ()() */
int main()
{
B b;
b(3);
}
WB
PS: g++ -v t1a.cpp (first program)
Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__ t1a.cpp /tmp/cc8Uj6oO.ii
GNU CPP version 2.95.2 19991024 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/../../../../include/g++-3
/usr/local/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/../../../../i586-pc-linux-gnu/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/include
/usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/cc1plus /tmp/cc8Uj6oO.ii -quiet -dumpbase t1a.cc -version -o /tmp/ccEBo5tt.s
GNU C++ version 2.95.2 19991024 (release) (i586-pc-linux-gnu) compiled by GNU C version 2.95.2 19991024 (release).
as -V -Qy -o /tmp/ccsxW3bu.o /tmp/ccEBo5tt.s
GNU assembler version 2.9.1 (i386-redhat-linux), using BFD version 2.9.1.0.23
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/crtbegin.o -L/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2 -L/usr/local/lib /tmp/ccsxW3bu.o -lstdc++ -lm -lgcc -lc -lgcc /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/crtend.o /usr/lib/crtn.o
PS: g++ -v t1a.cpp (second program)
Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__ t1b.cpp /tmp/cc1QPttJ.ii
GNU CPP version 2.95.2 19991024 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/../../../../include/g++-3
/usr/local/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/../../../../i586-pc-linux-gnu/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/include
/usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/cc1plus /tmp/cc1QPttJ.ii -quiet -dumpbase t1b.cc -version -o /tmp/ccsOUe0l.s
GNU C++ version 2.95.2 19991024 (release) (i586-pc-linux-gnu) compiled by GNU C version 2.95.2 19991024 (release).
t1b.cpp: In function `int main()':
t1b.cpp:33: no match for call to `(B) (int)'
t1b.cpp:18: candidates are: void B::operator ()()