Return 函数中的新内容返回 shared_ptr

Return new something in function returning a shared_ptr

我有一个函数,它 return 是一个 shared_ptr 到一个 const 对象。返回一个由指针 return 构造的 shared_ptr 由 operator new 工作,但是 returning 该指针直接导致编译错误:

Error   3   error C2664: 'std::shared_ptr<_Ty>::shared_ptr(std::nullptr_t)' : cannot convert parameter 1 from 'script::float_data *' to 'std::nullptr_t'    c:\xxx\value.cpp    59

这是导致错误的代码:

shared_ptr< const data > float_data::operator + ( shared_ptr< const data > rhs ) const
{
    int rhs_as_int; float rhs_as_float;

    switch( rhs->to_numeric( rhs_as_int, rhs_as_float ) )
    {
    case E_INT:
        return new float_data( val + rhs_as_int );
    case E_FLOAT:
        return new float_data( val + rhs_as_float );
    }
}

而 类 是:

class data
{
public:

    enum type
    {
        E_INT,
        E_FLOAT,
        E_STRING
    };

public:

    virtual ~data() { }

public:

    virtual std::shared_ptr< const data > operator + ( std::shared_ptr< const data > rhs ) const = 0;

    virtual std::shared_ptr< const data > operator - ( std::shared_ptr< const data > rhs ) const = 0;

    virtual std::shared_ptr< const data > operator * ( std::shared_ptr< const data > rhs ) const = 0;

    virtual std::shared_ptr< const data > operator / ( std::shared_ptr< const data > rhs ) const = 0;

    virtual std::shared_ptr< data > operator = ( std::shared_ptr< const data > rhs ) = 0;

public:

    virtual type to_numeric( int & as_int, float & as_float ) const = 0;
};

class int_data : public data
{
private:

    int val;

public:

    int_data( int i );

public:

    virtual std::shared_ptr< const data > operator + ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator - ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator * ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator / ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< data > operator = ( std::shared_ptr< const data > rhs );

public:

    virtual type to_numeric( int & as_int, float & as_float ) const;
};

class float_data : public data
{
private:

    float val;

public:

    float_data( float f );

public:

    virtual std::shared_ptr< const data > operator + ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator - ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator * ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator / ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< data > operator = ( std::shared_ptr< const data > rhs );

public:

    virtual type to_numeric( int & as_int, float & as_float ) const;
};

class string_data : public data
{
private:

    std::string val;

public:

    string_data( const char * s );

public:

    virtual std::shared_ptr< const data > operator + ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator - ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator * ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< const data > operator / ( std::shared_ptr< const data > rhs ) const;

    virtual std::shared_ptr< data > operator = ( std::shared_ptr< const data > rhs );

public:

    virtual type to_numeric( int & as_int, float & as_float ) const;
};

我不认为这是 C++11 特有的东西,但我是 C++11 的新手,所以我不确定。我不明白为什么 return 指针的两种方法都不起作用,以及为什么编译器会自动选择期望 nullptr_t.

的构造函数

那是因为 the constructor of std::shared_ptr is explicit,因此在 return 语句中,编译器在尝试构造 returned 时无法将原始指针隐式转换为 std::shared_ptr目的。你 必须 return 一个 std::shared_ptr 代替。

您看到奇怪的错误是因为编译器试图将原始指针参数与非显式构造函数匹配 (number 5 here)

constexpr shared_ptr( std::nullptr_t );

虽然 g++/clang++ 对诊断问题更有帮助,但示例错误:

error: could not convert from 'int*' to 'std::shared_ptr'