문제

나는 basic_logger 예제와 같이 클래스당 로그 소스를 원합니다. 문서

이것은 작동하지만 다음과 같이 const 멤버 함수에서 무언가를 로깅할 때 constness에 대해 불평하는 컴파일러 오류가 발생합니다.

#include "boost/log/sources/channel_logger.hpp"
#include "boost/log/common.hpp"
#include "boost/log/utility/init/to_console.hpp"
#include "boost/log/utility/init/common_attributes.hpp"

class Test
{
    public:
        // this works, not a const function
        void test1()
        {
            BOOST_LOG(m_logger) << "Test 1";
        }

        // this will not compile, const function
        void test2() const            
        {
            BOOST_LOG(m_logger) << "Test 2";
        }
    private:
        boost::log::sources::channel_logger<std::string> m_logger;
};

int main()
{
    boost::log::add_common_attributes();
    boost::log::init_log_to_console();

    Test t;
    t.test1();
    t.test2();
    return 1;
}

이 예에서는 Test::test2 const 및 m_logger 아니다. Test::test1 잘 작동합니다.

이 문제를 어떻게 깔끔하게 해결할 수 있습니까? const_cast, mutable 등.

도움이 되었습니까?

해결책

로그가 클래스에 저장되어 있으면 당연히 수정할 수 없습니다. const 문맥.

당신이 다른 것을 요구했다는 것을 알고 있지만 솔직히 그렇게 생각합니다 mutable 여기에 적절한 솔루션이 있습니다.이것이 바로 사용 사례입니다. mutable 존재합니다.

다른 팁

변경 가능한 로거를 클래스 외부로 이동해야 합니다. 그러면 모든 것이 제대로 작동합니다.또는 불변의 로거 객체를 제공하는 다른 것을 사용합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top