c++/7686: template compilation failure on unused method

leick.robinson@motorola.com leick.robinson@motorola.com
Thu Aug 22 12:06:00 GMT 2002


>Number:         7686
>Category:       c++
>Synopsis:       template compilation failure on unused method
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Thu Aug 22 12:06:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     leick.robinson@motorola.com
>Release:        gcc version 3.0.4
>Organization:
>Environment:
SUNW,Ultra-60
SunOS 5.8
>Description:
C++ does not instantiate a member function for a template
class unless the member function is actually used.  If the 
member function is not instantiated, the compiler does not 
check the member function (or rather, this particular 
instantiation) for compilation errors.

So, in the code example that follows, although the second
form of foo, foo(T t), would obviously not compile for
A<void>, since it is never called, A<void>::foo(void t) is
never instantiated, and in the past this has not caused
any problems with compilation.

This assumes that the rule for an uninstantiated member 
function extends to its declaration.  I am not certain
whether the C++ standard is clear on this point.

This compiles and runs without difficulties on gcc 2.95.2.
However, it fails to compile for gcc 3.0.4 (with the error
message:  In instantiation of `A<void>': invalid parameter
type `void' in declaration `void A<T>::foo(T)'

>How-To-Repeat:
// Compile this code:

#include <iostream>
using namespace std;

template <class T>
class A {
public:
   void foo()
   {
      cout << "Calling foo()" << endl;
   }

   void foo(T t)
   {
      cout << "Calling foo(T)" << endl;
   }
};

int main()
{
   A<int> a;  // Just to show that it works normally
   a.foo();
   a.foo(3);

   A<void> b;  // This instantiation compiles with 2.95.2,
               // but not with 3.0.4
   b.foo();
}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list