Changes between Version 1 and Version 2 of Programming/Cpp/StandardLibraryGotchaIncludeHeadersExplicitly


Ignore:
Timestamp:
Feb 19, 2016, 4:09:42 PM (8 years ago)
Author:
Vijay Varadan
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Programming/Cpp/StandardLibraryGotchaIncludeHeadersExplicitly

    v1 v2  
    44[[Image(htdocs:images/cpp/tcpppl.jpg, alt="tcpppl", align=center)]]
    55
    6 I was using the `boost::log` library component and when compiling my code, I ran into a rather odd error converting a derived shared pointer to a base class shared pointer, i.e. implicitly converting a **`shared_ptr<std::ofstream>`** to a **`const shared_ptr<std::ostream> &amp;`**. Here's the code snippet, with the line causing the compiler error highlighed. Note that code has been simplified for clarity.
     6I was using the `boost::log` library component and when compiling my code, I ran into a rather odd error converting a derived shared pointer to a base class shared pointer, i.e. implicitly converting a **`shared_ptr<std::ofstream>`** to a **`const shared_ptr<std::ostream> &`**. Here's the code snippet, with the line causing the compiler error highlighed. Note that code has been simplified for clarity.
    77
    88{{{#!cpp
     
    1212    sinks::text_ostream_backend> TextSink;
    1313
    14 bool Initialize(const std::string &amp;logFile)
     14bool Initialize(const std::string &logFile)
    1515{
    16     bool didSucceed = false;
     16    bool didSucceed = false;
    1717
    1818    try
    1919    {
    20         auto sink = boost::make_shared<TextSink>();
    21         auto lfstream 
    22             = boost::make_shared<std::ofstream>(logFile);
     20        auto sink = boost::make_shared<TextSink>();
     21        auto lfstream
     22            = boost::make_shared<std::ofstream>(logFile);
    2323        sink->locked_backend()->add_stream(lfstream); // <-- COMPILER ERROR HERE
    2424        core::get()->add_sink(sink);
    25         BOOST_LOG(lgr_gtl) << "LogSystem initialized...";
     25        BOOST_LOG(lgr_gtl) << "LogSystem initialized...";
    2626        core::get()->flush();
    2727 
    28         didSucceed = true;
     28        didSucceed = true;
    2929    }
    30     catch (const std::exception& ex)
     30    catch (const std::exception& ex)
    3131    {
    3232        std::cout
    33             << "LogSystem::Initialize() failed."
    34             << std::endl;
    35         std::cout << "Details:" << std::endl;
    36         std::cout << ex.what() << std::endl;
     33            << "LogSystem::Initialize() failed."
     34            << std::endl;
     35        std::cout << "Details:" << std::endl;
     36        std::cout << ex.what() << std::endl;
    3737    }
    38     return didSucceed;
     38    return didSucceed;
    3939}
    4040}}}
     
    4343the comment `COMPILER ERROR HERE`:
    4444
    45 {{{#!cpp
    46 error C2664: 'void boost::log::v2s_mt_nt6::sinks::basic_text_ostream_backend<char>::add_stream(const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &amp;)': cannot convert argument 1 from 'boost::shared_ptr<T>' to 'const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &amp;'
     45{{{
     46error C2664: 'void boost::log::v2s_mt_nt6::sinks::basic_text_ostream_backend<char>::add_stream(const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &)': cannot convert argument 1 from 'boost::shared_ptr<T>' to 'const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &'
    4747    with
    4848    [
     
    6262
    6363{{{#!cpp
    64 typedef basic_ofstream<char, char_traits<char> > ofstream;
     64typedef basic_ofstream<char, char_traits<char> > ofstream;
    6565}}}
    6666
     
    7777
    7878{{{#!cpp
    79 template<class _Elem, class _Traits> class basic_ofstream
    80     : public basic_ostream<_Elem, _Traits>
     79template<class _Elem, class _Traits> class basic_ofstream
     80    : public basic_ostream<_Elem, _Traits>
    8181{ // output stream associated with a C stream
    8282...