yohhoyの日記

技術的メモをしていきたい日記

括弧を使ったADL回避

本の虫: ADLへの対処方法で見かけた、下記の「括弧を使ったADL回避」に関するメモ。

ADLを回避する方法として、括弧を使う方法がある。

(print)( x ) ;

N3337を調べたら3.4.2/p1に直接例示があった(下線部が根拠)。関数名を括弧で括ることでunqualified-idではなくなり、ADLの適用条件から外れる。

When the postfix-expression in a function call (5.2.2) is an unqualified-id, other namespaces not considered during the usual unqualified lookup (3.4.1) may be searched, and in those namespaces, namespace-scope friend function declarations (11.3) not otherwise visible may be found. These modifications to the search depend on the types of the arguments (and for template template arguments, the namespace of the template argument). [ Example:

namespace N {
  struct S { };
  void f(S);
}

void g() {
  N::S s;
  f(s);    // OK: calls N::f
  (f)(s);  // error: N::f not considered; parentheses
           // prevent argument-dependent lookup
}

-- end example ]

関数呼び出し(function call)とpostfix-expressionについては13.3.1.1にて下記の通り、

In a function call (5.2.2)
 postfix-expression ( expression-listopt )

unqualified-idは5.1.1にて下記の通り定義される。

unqualified-id:
 identifier
 operator-function-id
 conversion-function-id
 literal-operator-id
 ~ class-name
 ~ decltype-specifier
 template-id