[cfe-dev] PATCH: C++ Function Overloading (v2)

Doug Gregor doug.gregor at gmail.com
Tue Oct 21 11:48:26 PDT 2008

On Tue, Oct 21, 2008 at 11:23 AM, Argiris Kirtzidis <akyrtzi at gmail.com> wrote:
> Hmm.. this is taken verbatim from C++ 03 14.6p9:
> void f(char);
> template<class T> void g(T t)
> {
> f(1); // f(char)
> f(T(1)); //dependent #1
> f(t); //dependent #2
> dd++; //not dependent // error: declaration for dd not found
> }
> void f(int);
> double dd;
> void h()
> {
> g(2); //will cause one call of f(char) followed
> // by two calls of f(int)
> g('a'); //will cause three calls of f(char)
> }
> Note that it says "g(2)" will cause one call of f(char) followed by two
> calls of f(int). By regarding the 'f', inside the template, just a
> FunctionDecl (the "f(char)" one), won't this result to 3 calls of f(char) ?

Yeah, the example is correct. This was noted in DR 197:


> Doesn't that mean that 'f' is
> not a simple FunctionDecl (since it's actually unbound) ?

It'll be a FunctionDecl in the AST. Then, when we actually go to
resolve the call, the set of candidate functions will include "f" and
anything found via ADL (which, in this case, is nothing).

Granted, this does mean that one can have a call in the AST for a
template where the function to be called is an identifier that can't
be resolved until instantiation time, e.g.,

template<typename T> void f(T x, T y) {
  swap(x, y); // there are no declarations of swap(), but that's okay
since it's a dependent name

BTW, I've committed my overloading patches basically as-is, because I
have some other changes coming that depend on them. However, I don't
consider the OverloadedFunctionDecl issue to be resolved; we should be
able to tweak its representation or how it's used without affecting
much other code, and it should be easier to experiment with it now
that it's in the tree.

  - Doug

More information about the cfe-dev mailing list