1

This is my code:

namespace Bar {
template<typename...Types>
class Foo {
public:
    template<class T>
    friend T* get_if(Foo<Types...>& f) {
       return nullptr;
    }
};
}
int main() {
  Bar::Foo<int, double> f;
  get_if<int>(f);
  return 0;
}

From my understanding get_if is an hidden friend and it can be found only by ADL. However get_if is not found (at least compiling with GCC 7.4.1), why?

greywolf82
  • 21,813
  • 18
  • 54
  • 108

1 Answers1

1
get_if<int>(f);

the problem is here, you tried to perform the instantiation by specifying the type, which needs the name of get_if<int> before the parameter is known

namespace Bar {
template<typename...Types>
class Foo {
public:
    template<class T>
    friend int* get_if(T a, Foo<Types...>& f) {
       return nullptr;
    }
};
}
int main() {
  Bar::Foo<int, double> f;
  get_if(5, f);
  return 0;
}

in contrast, this works.

con ko
  • 1,368
  • 5
  • 18