问题
I am currently working on a concept of Object known in Java or C# for C++. It would be similar to variant type like boost::any, however having wider functionality. For that purpose I am using boost::shared_ptr
to internaly store actual data and I wanted to provide Return Type Resolver idiom for easily obtaining this data, as it is stored in actual implementation. I know I could use boost::shared_ptr
automatic conversion during assigment operator or constructor but as I said shared_ptr
is not avaiable at this stage.
Implementing RtR I have encountered a problem with linux platform. For simplicity of code I provide just a simple code which reflects what I want to do basically and what is working under VS2010 and not under GCC. Any comments or solutions woud be appriciate.
struct RtR
{
template<typename Ptr>
operator Ptr()
{
return Ptr();
}
template<typename Ptr>
operator Ptr() const
{
return Ptr();
}
};
class TestRtR
{
void test()
{
boost::shared_ptr<int> intPtr(new int);
intPtr = get();
}
void test() const
{
boost::shared_ptr<const int> intPtr(new int);
intPtr = get();
}
RtR get()
{
RtR ret;
return ret;
}
const RtR get() const
{
const RtR ret;
return ret;
}
};
As I said - if You compile it under VS2010 everything goes ok, but under linux I get:
In member function ‘void TestRtR::test()':
error: ambiguous overload for ‘operator=’ in ‘intPtr = TestRtR::get()’
note: candidates are:
note: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(const boost::shared_ptr<T>&) [with T = int, boost::shared_ptr<T> = boost::shared_ptr<int>]
note: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(boost::shared_ptr<T>&&) [with T = int, boost::shared_ptr<T> = boost::shared_ptr<int>]
note: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(boost::detail::sp_nullptr_t) [with T = int, boost::shared_ptr<T> = boost::shared_ptr<int>, boost::detail::sp_nullptr_t = std::nullptr_t]
In member function ‘void TestRtR::test() const’:
error: ambiguous overload for ‘operator=’ in ‘intPtr = TestRtR::get()’
note: candidates are:
note: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(const boost::shared_ptr<T>&) [with T = const int, boost::shared_ptr<T> = boost::shared_ptr<const int>]
note: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(boost::shared_ptr<T>&&) [with T = const int, boost::shared_ptr<T> = boost::shared_ptr<const int>]
note: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(boost::detail::sp_nullptr_t) [with T = const int, boost::shared_ptr<T> = boost::shared_ptr<const int>, boost::detail::sp_nullptr_t = std::nullptr_t]
Are definitions of boost::shared_ptr
different under GCC and VS2010? What is the ground of this ambiguity and how to solve it?
来源:https://stackoverflow.com/questions/16981131/boostshared-ptr-and-return-type-resolver-idiom