2

I have been reading the interesting topic Why Not Specialize Function Templates? and somewhere encountered the following example.

struct Base{};

template<class T>
void f(T) { cout << "foo"; }

template<class T>
void functionSelector(T t)
{
    f(Base());
    f(t);
}

void f(Base) { cout << "bar"; }

int main ()
{
    functionSelector(Base());
    return 0;
}

This small program outputs

foobar

and I definitely could not understand why it didn't output

barbar

Any help would be appreciated.

Community
  • 1
  • 1
crbah
  • 338
  • 4
  • 12
  • 1
    The term you want to Google is` 2 phase lookup` – JVApen Jan 27 '20 at 22:13
  • @JVApen That's perfect!! Thank you! – crbah Jan 27 '20 at 22:19
  • 1
    I guess [this comment](https://stackoverflow.com/questions/7767626/two-phase-lookup-explanation-needed#comment9456869_7767726) explains the situation – crbah Jan 27 '20 at 22:34
  • 2
    Move `void f(Base) { cout << "bar"; }` above `template functionSelector` to get the expected behaviour. – Paul Sanders Jan 27 '20 at 22:34
  • @crbah I'm glad it starts to make sense to you, I'm still searching for a good explanation to grasp it completely, especially if ADL comes into play – JVApen Jan 27 '20 at 22:41

0 Answers0