このコードの壊れた部分を機能させるには助けが必要です。
文字列に基づいて 2 つの関数 (異なる値の型を返す) をタグ付けしてディスパッチするにはどうすればよいですか?
文字列でディスパッチする目的で全体のコードを単純化できる場合は、推奨事項を提示してください。TY.
要件:
- 文字列に基づくディスパッチ
- Rectangle オーバーロードは int を返す必要があり、Circle オーバーロードは std::string を返す必要があります
- Rectangle_Type から int へ、および Circle_Type から std::string へのマッピングは修正され、コンパイル時に認識されます。私の問題の一部は、std::map が実行時の構成要素であることです。std::string をコンパイル時の構成要素にタグ付けする方法がわかりません。
- 必要に応じて、実行時の解決は問題ありません。ただし、ディスパッチは、解決された列挙型/型に基づいて、さまざまな戻り値の型を許可する必要があります。
コード
#include <map>
#include <string>
#include <iostream>
struct Shape { };
struct Rectangle_Type : public Shape { using value_type=int; };
struct Circle_Type : public Shape { using value_type=std::string; };
Rectangle_Type Rectangle;
Circle_Type Circle;
static std::map<std::string,Shape*> g_mapping =
{
{ "Rectangle", &Rectangle },
{ "Circle", &Circle }
};
int tag_dispatch( Rectangle_Type )
{
return 42;
}
std::string tag_dispatch( Circle_Type )
{
return "foo";
}
int
main()
{
std::cerr << tag_dispatch( Circle ) << std::endl; // OK
std::cerr << tag_dispatch( Rectangle ) << std::endl; // OK
#define BROKEN
#ifdef BROKEN
std::cerr << tag_dispatch( (*g_mapping["Rectangle"]) ) << std::endl;
std::cerr << tag_dispatch( (*g_mapping["Circle"]) ) << std::endl;
#endif
}