朋友功能模板查找
friend function template 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
}
但是,GCC给出以下错误:
main.cpp: In function 'int main()':
main.cpp:10:5: error: 'foo' was not declared in this scope
foo<1>(t);
^~~
然后,我有三个问题。
- 应该根据标准找到
template<int N> foo
吗? - 为什么在
foo<1>
没有的情况下找到foo
? - 除了在外面定义
foo
外,还有解决方法吗?
https://en.cppreference.com/w/cpp/language/adl
虽然可以通过ADL解决函数调用,即使普通查找一无所获,但使用明确指定的模板参数对函数模板进行函数调用要求,需要普通查找的模板声明(否则,它是语法错误会遇到一个未知名称,然后是一个不太明显的字符)(直到C 20)
在C 20模式中,您的代码编译罚款,演示:https://gcc.godbolt.org/z/svdfw9drf
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- C++没有标准功能的立方体根查找器
- STL查找功能从Deque返回了什么
- 使用C RTTI(内置)通过字符串查找功能指针
- Trie 查找/添加功能无法正常工作
- 模板功能中的名称查找规则
- 功能查找和名称空间
- C 功能:读取直到文件结束 - 查找代码中的错误
- 如何查找从哪里导入程序C++特定功能
- Qt使用QWebEngine查找单词功能
- 我如何避免使用依赖于参数的查找明确专门化模板化功能
- C 与查找功能一起使用时,迭代器为什么行为会有所不同
- 朋友功能模板查找
- 如何分析、查找和修复C++功能中的安全漏洞?
- 链表查找功能C++
- 查找功能不起作用
- 使用设置查找功能,未找到运算符
- 地图的查找功能得到错误的结果
- 不确定查找功能在 CPP 中的工作原理
- 我是否正确使用了 std::map 的查找功能?想要访问类数据