gpt4 book ai didi

c++ - ADL,普通查找和依赖函数调用不起作用oO

转载 作者:太空狗 更新时间:2023-10-29 23:18:07 26 4
gpt4 key购买 nike

这里给出了简单的代码,它应该根据 C++ 标准工作我相信:

template<typename T> 
void foo(T x)
{
bar(x);
void bar(int);
}

void bar(int) { }

int main()
{
foo(0);
}

错误来自 GCC 4.7:

‘bar’ was not declared in this scope, and no declarations were foundby argument-dependent lookup at the point of instantiation

但在 C++ 标准中是这样写的。 § 14.6.4.2:

For a function call that depends on a template parameter, thecandidate functions are found using the usual lookup rules (3.4.1,3.4.2, 3.4.3) except that:

— For the part of the lookup using unqualified name lookup (3.4.1) or qualified name lookup (3.4.3), only function declarations from the template definition context are found.

可能是我看错了,谁能指正一下?

最佳答案

您应该将“bar”的声明移到顶部。因为在模板被定义(未实例化)的地方,在调用“bar”之前,它还没有被声明。

关于c++ - ADL,普通查找和依赖函数调用不起作用oO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14787818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com