2015-11-12 10 views
6

は、以下のコードを検討べきではない:これは、関数呼び出しの周りに薄いラッパーであるデフォルト関数パラメータ値が、(GCC)

#include <utility> 

void f(int, int); 
void g(int, int); 

struct functor 
{ 
    template<typename... T> 
    void operator()(T&&... params) 
    { 
     return f(std::forward<T>(params)...); 
    } 
}; 

int main() 
{ 
    functor()(1); // can use the default value here, why?! 
    // g(1); // error here as expected, too few arguments 
} 

void f(int a, int b = 42) {} 

void g(int a, int b = 24) {} 

を。しかし、functor::operator()の内部では、fは、既知の第2パラメータ(デフォルトではmainの後にしか表示されません)のデフォルト値を持たないため、コードをコンパイルするべきではありません。 G ++ 5.2はかかわらず、正常にコンパイルしますが、打ち鳴らすが、++ 1が正しく二相名の検索を行うコンパイラのために期待する期待のメッセージを出してくれる:

error: call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup return f(std::forward(params)...);

これはgccのバグですか、私はここで何かが足りないのですか?つまり、fの定義の後のインスタンス化のポイントはmain()ですか?しかし、この場合でさえ、それは機能しません。第2段階では、関数はADLを介してのみ見つけることができますが、ここではそうではありません。

+2

翻訳終了単位も、インスタンス化の有効なポイントです。 – Jarod42

+0

はい、それでも動作しません。なぜなら、第2段階ではADLだけが実行されるからです。 – vsoftco

+0

ファイルの終わりにインスタンス化すると、デフォルトパラメータを持つ 'f'の定義が見えます...プログラムが不正な形式でないかどうかわかりません... – Jarod42

答えて

1

[temp.dep.candidate]

For a function call where the postfix-expression is a dependent name, the candidate functions are found using the usual lookup rules ([basic.lookup.unqual], [basic.lookup.argdep]) except that:

  • For the part of the lookup using unqualified name lookup ([basic.lookup.unqual]), only function declarations from the template definition context are found.

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

If the call would be ill-formed or would find a better match had the lookup within the associated namespaces considered all the function declarations with external linkage introduced in those namespaces in all translation units, not just considering those declarations found in the template definition and template instantiation contexts, then the program has undefined behavior.

注関与タイプが(関連する名前空間の彼らのセットが空である)基本的なものとしてADLにも、ここで働いていないこと。

関連する問題