如何解决boost :: shared_ptr和使用std :: shared_ptr之间的冲突?

时间:2011-01-13 16:20:35

标签: c++ boost c++11

如果我在此代码段中从boost :: shared_ptr更改为std :: shared_ptr,我将收到链接器错误。

#include <iostream>
#include <sstream>
#include <iterator>
#include <cctype>
#include <cmath>

#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>

#include <functional>
#include <utility>
#include <numeric>

#include <boost/assign.hpp>
#include <boost/assign/std/vector.hpp>

#include <boost/algorithm/string.hpp>

#include <boost/test/included/unit_test.hpp>
#include <boost/test/included/unit_test_framework.hpp>
#include <boost/bind.hpp>

//using namespace std;
//using namespace boost;

using std::string;
using std::ostringstream;
using namespace boost::assign;
using namespace boost::unit_test;

template<typename T> string to_string( T data ) { ostringstream ost; ost << data; return ost.str(); }

class TwoStringMasks {
public:
    string shortestCommon( string s1, string s2 ) {
        //if( s1.find( "*" ) != 0 ||
        return "";
    }
};

class two_string_masks_test {
public:
    void test_case_one() {
        string str = "TOPCODER*";
        BOOST_CHECK_EQUAL( str.find( "*" ), str.length() - 2 );
    }
};

test_suite* init_unit_test_suite( int argc, char* argv[] ) {
    boost::shared_ptr<two_string_masks_test> tester( new two_string_masks_test );
    framework::master_test_suite().add(  
        BOOST_TEST_CASE( boost::bind( &two_string_masks_test::test_case_one, tester ) ) ); 
    return 0;
}

错误:

Error   12  error C2784: 'T *boost::get_pointer(T *)' : could not deduce template argument for 'T *' from 'std::tr1::shared_ptr<_Ty>'   c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   10  error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   11  error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   8   error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   9   error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   6   error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   7   error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   13  error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>'   c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   14  error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>'   c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   2   error C2784: 'optional<T>::pointer_type boost::get_pointer(boost::optional<T> &)' : could not deduce template argument for 'boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>'  c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   3   error C2784: 'optional<T>::pointer_type boost::get_pointer(boost::optional<T> &)' : could not deduce template argument for 'boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>'  c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   4   error C2784: 'optional<T>::pointer_const_type boost::get_pointer(const boost::optional<T> &)' : could not deduce template argument for 'const boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>'    c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report
Error   5   error C2784: 'optional<T>::pointer_const_type boost::get_pointer(const boost::optional<T> &)' : could not deduce template argument for 'const boost::optional<T> &' from 'std::tr1::shared_ptr<_Ty>'    c:\program files\boost\boost_1_44\boost\bind\mem_fn_template.hpp    40  1   NET Report

我做错了什么?我猜测std命名空间和boost命名空间之间存在冲突,但我不知道如何修复它?任何的想法?

谢谢,

4 个答案:

答案 0 :(得分:13)

看起来你需要添加一个get_pointer重载来使用std::shared_ptr和boost :: bind。

namespace boost { 
  template<class T> const T* get_pointer(const std::shared_ptr<T>& ptr) 
  {
    return ptr.get();
  }

  template<class T> T* get_pointer(std::shared_ptr<T>& ptr)
  {
    return ptr.get();
  }
}

如果您使用std::bind,我认为这已经有效了。或者使用tester.get()中的bind

答案 1 :(得分:6)

Boost和TR1共享指针是单独的实现,并且不兼容 - 选择一个或另一个并专门使用它。

答案 2 :(得分:3)

boost :: bind不知道如何处理tr1 :: shared_ptr它知道如何处理boost :: shared_ptr。

您可以尝试tr1 :: bind,它可能与tr1 :: shared_ptr一起使用。

唯一的问题是它是否适用于BOOST_TEST_CASE。如果它只是期望一个“runnable”,那么tr1 :: function就像一个“runnable”一样可以作为一个提升者。

答案 3 :(得分:1)

您是否注意到未明确包含&lt;记忆&gt;标题(也不是&lt; boost / shared_ptr.hpp&gt;)?

如果您提供了更多信息,则可以更轻松地提供帮助。

  1. 您使用的是哪种编译器?
  2. 您是否希望使用来自boost的编译器中的tr1?
  3. 你想访问shared_ptr作为std :: tr1 :: shared_ptr还是只是std :: shared_ptr?