0

編集:詳細については、この質問に対する私自身の回答を参照してください。これは、C++ の問題ではなく、Eclipse Juno のバグであることが判明しました。 それにもかかわらず、質問は他の C++ テンプレート ユーザーにとって有用なトピックをカバーしています。

「テンプレート」型の引数と「非 temlate」型の他の引数を持つテンプレート クラスを作成したい場合、どのように指定すればよいですか?

例: 実装または itoa() ですが、複数の型があり、パディングして文字列を返します...

編集: 定義内の var 名を修正しました。

   template <typename T>   std::string    Num2Str( T x, char pad = ' ', int width = 0 );
   template <typename T>   std::string    Num2Str( T x, char pad, int width )
   {
      static std::string   string;
      std::stringstream    ss;
      ss << std::setfill(pad) << std::setw(width) << x;
      string = ss.str();
      return string;
   }

編集: これは、コンパイラ/プラットフォーム、g++、VC++ で動作するはずです。

4

2 に答える 2

2

テンプレートのパラメーターと関数のパラメーターを混同していると思います。なぜこれだけではないのですか:

#include <sstream>
#include <iomanip>

template <typename T>   
std::string Num2Str( T x, char pad = ' ', int width = 0 )
{
    static std::string   string;
    std::stringstream    ss;
    ss << std::setfill(pad) << std::setw(width) << x;
    string = ss.str();
    return string;
}

void Test()
{
    auto s1 = Num2Str( 1.0 );
    auto s2 = Num2Str( 2, '-' );
    auto s3 = Num2Str( 3.0, ' ', 3 );
}
于 2013-05-01T23:03:15.880 に答える