簡體   English   中英

帶有模板函數的嵌套命名空間中的C ++ ADL

[英]C++ ADL in nested namespaces with template function

我對C ++中的標准ADL分辨率有疑問。

以下是解釋我的查詢的示例代碼:

#include <string>

// The mechanism:
namespace A {

 template< class C >
 ::std::string scope(const C*)
 { return "A"; }

 namespace B {

  template< class C >
  ::std::string scope(const C *foo)
  { return A::scope(foo)+"::B"; }

 } // namespace B
} // namespace A

::std::string scope(...)
{ return ""; }

// The test classes
struct foo {};
namespace A {
 struct foo {};
 namespace B {
  struct foo {};
 }
}

// The usage
int main()
{
  foo *Foo=0;
  A::foo *FooA=0;
  A::B::foo *FooB=0;

  scope(Foo);  // OK, returns ""
  scope(FooA); // OK, returns "A"
  scope(FooB); // On one compiler, OK returns "A::B" ; On another, compiler error "Ambiguous call" between A::scope() and A::B::scope()
}

那么,我的問題是關於ADL的標准是什么? 是否應該找到參數的父命名空間中的所有函數,或者只有參數的(嵌套)命名空間中可用的函數+全局函數?

該程序已經在MSVC 2008上進行了測試(並且使用SP編譯但不是沒有......)

根據標准,ADL工作(以幾個特殊規則為模)“好像”函數名稱前面是命名空間; 在你的最后一行中,查找應該在你編寫A::B::scope 哪個不在周圍的命名空間中查找。

請注意,即使在命名空間A::B ,也不存在歧義; A::BA::B::scope隱藏A::scope 非限定名稱查找在其首次查找名稱的范圍內停止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM