나는 몇 가지 방법이 좋아하는이 클래스는 다음이 (더) :RAII를 사용하여 캐치를 시도 하시겠습니까?
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 :
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 블록에서 함수의 몸을 둘러싸하려면 : 이런 예외를 사용하는 것이 좋습니다? (각 기능에서). 나는 익숙하지 않아 예외를 사용하므로 잘 모르겠습니다.
질문 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
왜 템플릿 기능과 동일한 템플릿 외 과부하가 있습니까? 그것들은 모두 인라인 (inline)이기 때문에 비언어적 인 것으로 논쟁을 가져가는 것이 중요하지 않습니다. – MSalters