c++/4205: function template can call other function with incorrect parameters

Wolfgang Bangerth bangerth@ticam.utexas.edu
Fri Nov 15 18:21:00 GMT 2002


This code compiles silently:
-------------------------------------
template<typename F> void quirk(F f) {
  (*f) (1);
}

void foo(int i, int j = 5){}
void bar(int i, int j)    {}

int main() {
  quirk(&foo); 
  quirk(&bar);
}
-------------------------------------

The assertion of the submitter is that the first quirk(&foo) is ok, since 
the call to (*f)(1) will substitute the second arg of foo by the default 
argument of that function. The second call would be wrong. It succeeds, of 
course, since the function quirk for exactly this template arg has already 
been compiled, and no re-compilation means no re-check.

However, I believe that already the first one is bogus. The template 
argument F of quirk is 
  void (*) (int, int),
so the call to (*p)(1) should be invalid. We should not know about default 
arguments in quirk, right? I'm surprised that default arguments are 
propagated to the template function. They don't appear in the type of 
quirk as well, since if I enter a
    std::cout << __PRETTY_FUNCTION__ << std::endl;
into that function, I only get 
    void quirk(F) [with F = void (*)(int, int)]
But that may of course have other roots.

Regards
  Wolfgang

-------------------------------------------------------------------------
Wolfgang Bangerth              email:           bangerth@ticam.utexas.edu
                               www: http://www.ticam.utexas.edu/~bangerth




More information about the Gcc-bugs mailing list