According to the standard, friend function declared and defined in class can only be find by ADL. So, I think the following code should compile.
template<int M>
struct test{
template<int N = 0>
friend void foo(test){}
};
int main(){
test<2> t;
foo(t);// compile
foo<1>(t);// error
}
However, gcc gives the following error:
main.cpp: In function 'int main()':
main.cpp:10:5: error: 'foo' was not declared in this scope
foo<1>(t);
^~~
Then, I have three problems.
template<int N> foo
be found according to the standard?foo
is found while foo<1>
is not?foo
outside?https://en.cppreference.com/w/cpp/language/adl
Although a function call can be resolved through ADL even if ordinary lookup finds nothing, a function call to a function template with explicitly-specified template arguments requires that there is a declaration of the template found by ordinary lookup (otherwise, it is a syntax error to encounter an unknown name followed by a less-than character) (until C++20)
In C++20 mode your code compiles fine, demo: https://gcc.godbolt.org/z/svdfW9drf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With