2

は私がのstd ::関数の引数に「中継」されているテンプレートパラメータの可変数、とテンプレート機能を持っているパラメータしかし、Visual Studioは、このエラーメッセージが表示され、赤でメインTEST1 -callsのすべてを強調:Visual Studioのエラーが

template<class... Args> void test1(const std::function<void(Args...)> &function) 

no instance of function template "test1" matches the argument list 
    argument types are: (lambda []void(float f)->void) 

この一方、エラーとして表示されません。

template<typename... Args> 
    void test2(Args... a) 
{} 
int main(int argc,char *argv[]) 
{ 
    test2<float>(1.f); 
    return EXIT_SUCCESS; 
} 

は、私が最初のケースのために、誤って何かをやっているか、これは偽陽性のですか?これはビジュアルスタジオ自体からの単なるエラーですが、コンパイラは警告をスローしません。

//編集:

私はちょうどG ++でLinux上でいくつかのテストをした - 5、そしてそれは私がすべてのコードをコンパイルできません:

[email protected]******:/var/projects# g++-5 -std=c++1y test.cpp 
test.cpp: In function ‘int main(int, char**)’: 
test.cpp:12:22: error: no matching function for call to ‘test1(std::nullptr_t)’ 
    test1<float>(nullptr); 
        ^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&) 
    void test1(const std::function<void(Args...)> &function) 
    ^
test.cpp:5:7: note: template argument deduction/substitution failed: 
test.cpp:12:22: note: mismatched types ‘const std::function<void(Args ...)>’ and ‘std::nullptr_t’ 
    test1<float>(nullptr); 
        ^
test.cpp:13:17: error: no matching function for call to ‘test1(void (*)(float))’ 
    test1<float>(&t); 
       ^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&) 
    void test1(const std::function<void(Args...)> &function) 
    ^
test.cpp:5:7: note: template argument deduction/substitution failed: 
test.cpp:13:17: note: mismatched types ‘const std::function<void(Args ...)>’ and ‘void (*)(float)’ 
    test1<float>(&t); 
       ^
test.cpp:16:3: error: no matching function for call to ‘test1(main(int, char**)::<lambda(float)>)’ 
    }); 
^
test.cpp:5:7: note: candidate: template<class ... Args> void test1(const std::function<void(Args ...)>&) 
    void test1(const std::function<void(Args...)> &function) 
    ^
test.cpp:5:7: note: template argument deduction/substitution failed: 
test.cpp:16:3: note: ‘main(int, char**)::<lambda(float)>’ is not derived from ‘const std::function<void(Args ...)>’ 
    }); 
^

答えて

1

あなたはテンプレートの構造体にテスト関数をラップする可能性が期待どおり問題を回避し、プログラムを動作させるために:

template<typename... Args> 
struct foo { 
    static void test1(const std::function<void(Args...)> &function) {} 
}; 

そして、それを呼び出す:

foo<float>::test1(nullptr); 
foo<float>::test1(&t); 
foo<float>::test1([](float f) -> void { 

}); 

それは間違いなくあなたのArgs...を防ぐことができます推測されることから。

1

のVisual Studio解析がありますコンパイラの背後にビットがあり、別々に実装されています。 VariadicのテンプレートはまだVisual Studioにはまだ新しくなっているので、おそらく既知の制限/バグです。

更新:

それも打ち鳴らすの最新バージョンで何が起こっているのか私にははっきりしていない:あなたは `` float`が、対応する非テンプレートコードにArgs...を修正しなかったので live exampleはコンパイルありません。また

、あなたはそれが仕事をしArgs...Argsに変更した場合。明確ではない私には、なぜ...

アップデート2:私はあなたの質問は良い答えで、重複していることが判明: variadic templates parameter matching in std::function

ずさんな要約:あなたはtest2<float>を書き、それより以下の手段test2<float,Args...>、さらなる変換を妨げる。

+0

ビジュアルスタジオに限らず(自分の編集を参照) – Silverlan

関連する問題