This is the mail archive of the gcc-bugs@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]

Re: c++/4205: function template can call other function with incorrectparameters


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



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