2

私の問題は次のとおりです

template<class T> MyClass
{
    MyClass(/* Lots of parameters with no problem */, const T& min = 0, const T& max = std::numeric_limits<T>::max());
    set(/* Lots of parameters with no problem */, const T& min = 0, const T& max = std::numeric_limits<T>::max());
    /* Lots of function with no problem */
}

std::stringすべての関数を再実装せずに、テンプレートクラスと互換性を持たせたいです。std::stringの場合は必要min = ""ですmax = ""。現在、たとえば0を文字列に変換できないため、クラッシュします。どうやってするか ?(コンストラクターとメインセッターのみを専門にできるとしたら、それは素晴らしいことです)。

4

4 に答える 4

3

私が推測するラッパーを作成しますか?:

template<typename T> struct ttraits
{
static T max(){
return std::numeric_limits<T>::max();
}
static T min(){
return std::numeric_limits<T>::min();
}
};

template<> struct ttraits<std::string>
{
static std::string max(){
return ""; //or whatever max is for you
}
static std::string min(){
return "";
}
于 2012-07-22T18:20:26.427 に答える
1

特殊なケースを処理するために、いつでも適切なオーバーロードを選択enable_ifできます。または、コードをより堅牢にする方法についてよりよく考えることができます。テンプレートパラメータの初期化に使用0することはお勧めできませんが、お勧めできませんT()

于 2012-07-22T17:57:31.447 に答える
1

numeric_limits標準のものにリダイレクトし、文字列に特化した独自のものを作成します。

于 2012-07-22T18:07:20.503 に答える
0
set(T & const p_Arg = Initializer<T>())

ここで、Initializerはサポートされているすべてのタイプに特化しています。

于 2012-07-22T18:23:08.057 に答える