gpt4 book ai didi

c++ - 从 xlC 的模板函数问题中查找静态函数

转载 作者:可可西里 更新时间:2023-11-01 16:34:35 26 4
gpt4 key购买 nike

当我在源代码中寻找有关编译问题的线索时,我遇到了这个 bug report (against Mozilla's JavaScript engine source)与函数查找相关。引用错误报告:

TypedArrayTemplate is (obviously) a template, and it is referencing INT_TO_JSVAL, a static inline function, without prefixing it with "::". This breaks xlC because it can not resolve INT_TO_JSVAL. The standard does not require that statics be considered if the unqualified name is not found in the context of the template arguments. g++ does this fallback, xlC does not.

来自编译器的信息性消息:

(I) Static declarations are not considered for a function call if the function is not qualified.

在我的例子中,失败的代码与此类似:

namespace N
{

static bool foo (std::string const &);

template <typename T>
void bar (T const &, std::string const & s)
{
// expected unqualified call to N::foo()
foo (s);
}

void baz (std::string const & s)
{
bar (s);
}

} // namespace N

xlC 实现的行为真的正确吗? 2003 年或 2011 年的标准在哪里谈到这个?

最佳答案

在 C++11 之前,这是正确的行为:模板中使用的名称的非限定名称解析被定义为仅查找具有外部链接的函数。

C++03 第 14.6.4.2 节候选函数 [temp.dep.candidate] 第 1 段:

For a function call that depends on a template parameter, if the function name is an unqualified-id but not a template-id, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:

  • For the part of the lookup using unqualified name lookup (3.4.1), only function declarations with external linkage from the template definition context are found.

  • For the part of the lookup using associated namespaces (3.4.2), only function declarations with external linkage found in either the template definition context or the template instantiation context are found.

在 C++11 中更改为:

For a function call that depends on a template parameter, the candidate 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.

  • For the part of the lookup using associated namespaces (3.4.2), only function declarations found in either the template definition context or the template instantiation context are found.

关于c++ - 从 xlC 的模板函数问题中查找静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17661308/

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