以下の方法の主な違いは何ですか? std::to_string
が定義されている場合、2つのうちの1つが問題を引き起こすケースはありますか?1つのテンプレート特殊化だけが役に立ちますか?
include <string>
using namespace std;
enum class eColor
{
Red
};
void to_string(eColor color)
{
}
template<typename C = eColor)
void to_string(C color)
{
}
int main()
{
to_string(eColor::Red); // assume only one of the above is defined
return 0;
}
上記のいずれかが望ましい場合はありますか?
テンプレート機能が自動的にテンプレート引数を推測:あなたはto_string
ような名前を使用する予定がある場合は特に奇妙である、あなたのusingディレクティブにより、名前空間の不必要な汚染のようなものにもかかわらず
テンプレートが混乱を招きます。 https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – juanchopanza
も参照してください。なぜテンプレートを使用したいのですか? – juanchopanza
私はテンプレートバージョンのために多くの点を見ません。しかし、あなたの例は単純化されているので、実装を示すべきです。 – user0042