A mysterious compilation error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

StackOverflow https://stackoverflow.com/questions/13630751

Question

I wanted to protect the access to a log file that I use for multithreaded logging with boostlog library.

I tried this stream class

class ThreadSafeStream
{
public:
    template <typename TInput>
    const ThreadSafeStream& operator<< (const TInput &tInput) const
    {
         // some thread safe file access    
         return *this;
    }
};

using it this way (text_sink is a boostlog object):

    //...
m_spSink.reset(new text_sink); 
text_sink::locked_backend_ptr pBackend = m_spSink->locked_backend();

const boost::shared_ptr< ThreadSafeStream >& spFileStream = boost::make_shared<ThreadSafeStream>();

pBackend->add_stream(spFileStream); // this causes the compilation error

and I get this mysterious error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

the whole compile error:

Log.cpp(79): error C2664: 'boost::log2_mt_nt5::sinks::basic_text_ostream_backend<CharT>::add_stream' : cannot convert parameter 1 from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T> &'
1>          with
1>          [
1>              CharT=char
1>          ]
1>          and
1>          [
1>              T=ThreadSafeStream
1>          ]
1>          and
1>          [
1>              T=std::basic_ostream<char,std::char_traits<char>>
1>          ]
1>          Reason: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'
1>          with
1>          [
1>              T=ThreadSafeStream
1>          ]
1>          and
1>          [
1>              T=std::basic_ostream<char,std::char_traits<char>>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I suspect that I am not well defining the operator<<()... but I don't find what is wrong.

this is the prototype of addStream: void add_stream(shared_ptr< stream_type > const& strm); with typedef std::basic_ostream< target_char_type > stream_type;

Was it helpful?

Solution

From the error message presuably pBackend->add_stream is expected a shared_ptr (!) to an ostream while you're giving it a shared_ptr to a ThreadSafeStream which is a totally unrelated type. You'll need to create an overloaded add_stream method that works with ThreadSafeStreams most likely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top