私はいくつかの方法が、以下の(そしてもっと)を好き持つクラスを持っている:RAIIを使用してキャッチを試みますか?
template<class T>
Logpp& operator<<(T const& obj)
{
*p_Stream << obj ;
return *this ;
}
Logpp& operator<<(char const* zString)
{
*p_Stream << zString ;
return *this ;
}
Logpp& operator<<(std::ostream& (*manip)(std::ostream&))
{
*p_Stream << manip;
return *this ;
}
私は、フォームのトライcatchブロック内の関数の本体を囲みたい:
Logpp& operator<<(std::ostream& (*manip)(std::ostream&))
{
try
{
*p_Stream << manip;
return *this;
}
catch(ios_base::failure& e)
{
//MyException has a stringstream inside and can use operator<<
throw MyException("IO failure writing to log file : ") << e.what() << endl;
}
}
Q1:このような例外を使用することをお勧めしますか? (各機能で)。私は例外を使用することに慣れていないので、わからない。
質問2:Q1への回答が肯定的な場合、冗長性を取り除くためにこれを行うことはできますか?
Logpp& operator<<(std::ostream& (*manip)(std::ostream&))
{
Catch<ios_base::failure> tc();
try
{
*p_Stream << manip;
return *this;
}
//destructor of tc will be written to catch the template exception type and rethrow as a MyException.
}
私はファイルとライン情報を埋め込むマクロと同様のことをします。マクロも使えますか? – Anycorn
テンプレート以外のオーバーロードがテンプレート関数と同じになるのはなぜですか?彼らはすべてインラインであるので、彼らは非参照として彼らの議論を取ることは重要ではありません。 – MSalters