cqd*_*234 5 c++ friend-function function-templates argument-dependent-lookup
根据标准,在类中声明和定义的友元函数只能由ADL查找.所以,我认为以下代码应该编译.
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
}
Run Code Online (Sandbox Code Playgroud)
但是,gcc给出以下错误:
main.cpp: In function 'int main()':
main.cpp:10:5: error: 'foo' was not declared in this scope
foo<1>(t);
^~~
Run Code Online (Sandbox Code Playgroud)
然后,我有三个问题.
template<int N> foo按照标准找到?foo发现foo<1>不是?foo外部之外还有解决方法吗?https://en.cppreference.com/w/cpp/language/adl
尽管即使普通查找没有找到任何内容,也可以通过 ADL 解析函数调用,但对具有显式指定模板参数的函数模板的函数调用要求存在普通查找找到的模板的声明(否则,这是语法错误遇到未知名称后跟小于号字符)(C++20 之前)
在 C++20 模式下,您的代码可以正常编译,演示: https: //gcc.godbolt.org/z/svdfW9drf