为什么 ADL 找不到函数模板?

发布于 2024-09-04 07:55:07 字数 347 浏览 13 评论 0原文

C++ 规范的哪一部分限制参数相关查找在关联命名空间集中查找函数模板?换句话说,为什么下面main中的最后一个调用无法编译?

namespace ns {
    struct foo {};
    template<int i> void frob(foo const&) {}
    void non_template(foo const&) {}
}

int main() {
    ns::foo f;
    non_template(f); // This is fine.
    frob<0>(f); // This is not.
}

What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile?

namespace ns {
    struct foo {};
    template<int i> void frob(foo const&) {}
    void non_template(foo const&) {}
}

int main() {
    ns::foo f;
    non_template(f); // This is fine.
    frob<0>(f); // This is not.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流年里的时光 2024-09-11 07:55:07

这部分解释一下:

C++ Standard 03 14.8.1.6

[注意:对于简单函数名称,即使函数名称在调用范围内不可见,参数相关查找 (3.4.2) 也适用。这是因为该调用仍然具有函数调用的语法形式(3.4.1)。但是,当使用具有显式模板参数的函数模板时,调用不具有正确的语法形式,除非在调用时存在可见的具有该名称的函数模板。如果没有这样的名称可见,则调用在语法上不正确,并且依赖于参数的查找不适用。如果某些此类名称可见,则应用参数相关查找,并且可能会在其他命名空间中找到其他函数模板。

namespace A {
  struct B { };
  template<int X> void f(B);
}
namespace C {
  template<class T> void f(T t);
}
void g(A::B b) {
  f<3>(b);    //ill-formed: not a function call
  A::f<3>(b); //well-formed
  C::f<3>(b); //ill-formed; argument dependent lookup
              // applies only to unqualified names
  using C::f;
  f<3>(b);    //well-formed because C::f is visible; then
              // A::f is found by argument dependent lookup
}

This part explains it:

C++ Standard 03 14.8.1.6:

[Note: For simple function names, argument dependent lookup (3.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (3.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.

namespace A {
  struct B { };
  template<int X> void f(B);
}
namespace C {
  template<class T> void f(T t);
}
void g(A::B b) {
  f<3>(b);    //ill-formed: not a function call
  A::f<3>(b); //well-formed
  C::f<3>(b); //ill-formed; argument dependent lookup
              // applies only to unqualified names
  using C::f;
  f<3>(b);    //well-formed because C::f is visible; then
              // A::f is found by argument dependent lookup
}
洋洋洒洒 2024-09-11 07:55:07

从 c++20 开始,adl 也可以与显式函数模板配合使用。这是建议:
P0846R0:不可见的 ADL 和函数模板< /a>:

我们提出了对查找规则的修订,而不是要求用户使用 template 关键字,以便正常查找不会产生结果或找到一个或多个函数的名称,并且后面跟着 aa "<; ”将被视为已找到函数模板名称并导致执行 ADL。

目前,只有 GCC 9 实现了此功能,因此您的示例可以编译。

现场演示

Since c++20, adl works also fine with explicit function template. Here is the proposal:
P0846R0: ADL and Function Templates that are not Visible:

Instead of requiring the user to use the template keyword, a revision to the lookup rules was proposed so that a name for which a normal lookup produces either no result or finds one or more functions and that is followed by a a "<" would treated as if a function template name had been found and would cause ADL to be performed.

Currently, only GCC 9 has implement this feature, so your example can compile.

live demo.

少女七分熟 2024-09-11 07:55:07

我想改进稍微接受的答案。 OP问题中尚不清楚,但标准中的重要部分(由Kornel引用)是这样的(强调我的):

但是当使用带有显式模板参数的函数模板时,调用没有正确的语法形式

因此禁止依赖 ADL 并使用显式模板参数。不幸的是,使用非类型模板参数需要使用显式参数(除非它们有默认值)。

下面是显示这一点的示例代码:

[live]

#include <string>
#include <utility>

namespace C {
  struct B { };
  template<class T> void f(T t){}
}

void g(C::B b) {
  f(b);           // OK
  //f<C::B>(b);   // ill-formed: not a function call, but only 
                  //  because explicit template argument were used

  std::string s;
  move(s);                      // OK
  //move<std::string&>(s);      // Error, again because 
                                //  explicit template argument were used
  std::move<std::string&>(s);   // Ok
}

int main()
{
 C::B b;
 g(b);
}

I would like to refine slightly accepted answer. It is not clear in the OP question, but the important part from the standard (cited by Kornel) is this (emphasis mine):

But when a function template with explicit template arguments is used, the call does not have the correct syntactic form

so what is prohibited is relying on ADL and using explicit template arguments. Unfortunately using non-type template arguments requires using explicit arguments (unless they have default values).

Below is sample code showing this.:

[live]

#include <string>
#include <utility>

namespace C {
  struct B { };
  template<class T> void f(T t){}
}

void g(C::B b) {
  f(b);           // OK
  //f<C::B>(b);   // ill-formed: not a function call, but only 
                  //  because explicit template argument were used

  std::string s;
  move(s);                      // OK
  //move<std::string&>(s);      // Error, again because 
                                //  explicit template argument were used
  std::move<std::string&>(s);   // Ok
}

int main()
{
 C::B b;
 g(b);
}
放血 2024-09-11 07:55:07

编辑:不,这是不对的。请参阅@Kornel 的回答


我不完全确定,但在查阅了 Stroustrup 的“C++ 编程语言”后,我认为附录 C 第 13.8.4 节可能是原因。

由于 frob 是一个模板,因此可以想象在调用它之后的某个时刻将其专门化为 i=0 。这意味着实现将留下两种可能的方式来选择调用哪个frob,因为它似乎可以在实例化时在处理结束时选择它翻译单位。

所以,我认为问题是你可以做

namespace ns {
    struct foo {};
    template<int i> void frob(foo const&) {}
}

int main() {
    ns::foo f;
    frob<0>(f);
    return 0;
}

namespace ns {
    template<> void frob< 0 >(foo const&) { /* Do something different*/ }
}

Edit: No, this is not right. See @Kornel's answer.


I'm not entirely sure but having consulted Stroustrup's "The C++ programming language" I think that Appendix C section 13.8.4 might be the cause.

Since frob is a template one could conceivably specialise it for i=0 at a point after you call it. This means that the implementation would be left with two possible ways of choosing which frob to call as it appears it can choose it at the point of instantiation or at the end of processing the translation unit.

So, I think the problem is you could do

namespace ns {
    struct foo {};
    template<int i> void frob(foo const&) {}
}

int main() {
    ns::foo f;
    frob<0>(f);
    return 0;
}

namespace ns {
    template<> void frob< 0 >(foo const&) { /* Do something different*/ }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文