テンプレート クラスのネストされた型を使用してテンプレート関数を実装したいと考えています。
ここで、非メンバーおよび非フレンド関数として実装する方が良いことを読みました。operator <<したがって、関数toStream()とtableToStream()外部を移動することにしましたMyClass:
template <typename T>
class MyClass
{
public:
typedef boost::dynamic_bitset<> BoolTable;
typedef std::vector<T> MsgTable;
private:
BoolTable b_;
MsgTable m_;
public:
const BoolTable& getB() const { return b_; }
const MsgTable & getM() const { return m_; }
std::ostream& toStream (std::ostream& os) const
{
os <<"Bool: "; tableToStream (os, getB()); os <<'\n';
os <<"Msg:"; tableToStream (os, getM()); os <<'\n';
return os;
}
template <typename TABLE>
std::ostream& tableToStream (std::ostream& os, const TABLE& table) const
{
for (int i=0; i < table.size(); ++i)
os << table[i] <<',';
return os;
}
};
template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T> mc)
{
return mc.toStream(os);
}
非メンバーおよび非フレンド関数に変換MyClass::toStream()するのは簡単です。operator <<
template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)
{
os <<"Bool: "; mc.tableToStream (os, mc.getB()); os <<'\n';
os <<"Msg:"; mc.tableToStream (os, mc.getM()); os <<'\n';
return os;
}
operator <<しかし、私は呼び出す代わりに単独で使用したいMyClass::tableToStream():
template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)
{
os <<"Bool: "<< mc.getB() <<'\n';
os <<"Msg:" << mc.getM() <<'\n';
return os;
}
関数についてはMyClass::tableToStream()、次の実装を使用できますが、関数が一般的すぎるため、ストリーム出力が混乱する可能性があります (任意の型を にすることができますTABLE)。
template <typename TABLE>
std::ostream& operator << (std::ostream& os, const TABLE& table)
{
for (int i=0; i < table.size(); ++i)
os << table[i] <<',';
return os;
}
したがって、ネストされた型に制限したいと思いMyClassます。MyClass::tableToStream()以下は、標準operator <<の非メンバーおよび非フレンド関数に変換する私の試みの1つです。
template <typename T, typename MyClass<T>::TABLE>
std::ostream& operator << (std::ostream& os, const TABLE& table)
{
for (int i=0; i < table.size(); ++i)
os << table[i] <<',';
return os;
}
しかし、エラーは約typename MyClass<T>::TABLEです。