2011-12-01 15 views
0

だから、OverLoad (link to downloadable svn directory, lib is header only)という素晴らしいライブラリがあります。それはそれに任意のタイプの機能を受け入れることができ、自動的にどちらを呼び出すかを決定することができます。それはブースト機能のようですが、より良い。 ここに2つのコードサンプルがあります(ブラウザはブーストsvnを表示できます)onetwoBoost Overload strange behavior .. `int(int)、int(std :: string)`が `int(int)、int(std :: string)、std :: string(std :: string)`とどのように違うのですか?

#include <string> 

#include <boost/detail/lightweight_test.hpp> 

#include <boost/overload.hpp> 

using boost::overload; 

template<class out, class in> 
out foo(in O) 
{ 
    std::cout << "yes we can!"; 
    return out(); 
} 

int main() 
{ 
    //// works 
    //overload<int (int), int (std::string)> f; 
    //// works 
    //int (*foo1) (int) = &foo<int, int>; 
    //int (*foo2) (std::string) = &foo<int, std::string>; 
    //f.set(foo1); 
    //f.set(foo2); 
    // or we can use 
    //// does also work 
    //f.set<int (int)>(&foo<int, int>); 
    //f.set<int (std::string)>(&foo<int, std::string>); 
    //// 

    overload<int (int), int (std::string), std::string (std::string) > f; 
    //// but when we do this 
    //f.set<int (int)>(&foo<int, int>); 
    //f.set<int (std::string)>(&foo<int, std::string>); 
    //f.set<int (std::string)>(&foo<std::string, std::string>); 
    //// or this: 
    int (*foo1) (int) = &foo<int, int>; 
    int (*foo2) (std::string) = &foo<int, std::string>; 
    std::string (*foo3) (std::string) = &foo<std::string, std::string>; 
    f.set(foo1); 
    f.set(foo2); 
    f.set(foo3); 
    //// we get compile error 

    BOOST_ASSERT(f(0) == 1); 
    BOOST_ASSERT(f("hi") == 2); // here we get Error 1 error C3066: there are multiple ways that an object of this type can be called with these arguments 

    return boost::report_errors(); 
} 

私はこの問題を回避する方法を知りましたか?

+1

'@' myWallJSON私はあなたが今注目していると確信しています。あなたはあなたの質問の書式設定/プレゼンテーションにもっと力を注ぐことができますか?あなたは、質問を美味しくするために多くの努力を払うことなく、たくさん質問しているようです。あなたの配慮ありがとう! – sehe

答えて

1

オーバーロードの解決は、パラメータの種類のみを考慮します。戻り値の型は考慮しません。したがって、過負荷の解決時にint (std::string)std::string(std::string)と区別できません。

このライブラリはC++言語のオーバーロード機能に依存する必要があるため、2つの機能を区別することもできません。

関連する問題