C++ Pimpl 习语不完整类型使用 std::unique_ptr
C++ Pimpl Idiom Incomplete Type using std::unique_ptr
对于演示该问题所需的大量代码,我深表歉意。我在使用带 std::unique_ptr 的 pimpl 习语时遇到问题。具体来说,当一个 class(具有 pimpl 的实现)在另一个具有 pimpl 的实现的复合 class 中用作成员数据时,问题似乎会发生。
我能找到的大多数答案都缺少 explicit destructor declaration,但正如您在这里看到的,我已经声明并定义了析构函数。
这段代码有什么问题,是否可以在不改变设计的情况下修改编译?
注意:错误似乎出现在 SomeComposite::getValue() 的定义中,并且编译器直到编译时才能看到错误。在 memory.h 中遇到错误,消息是 Invalid application of 'sizeof' to an incomplete type 'pimplproblem::SomeInt::impl'.
SomeInt.h
#pragma once
#include <iostream>
#include <memory>
namespace pimplproblem
{
class SomeInt
{
public:
explicit SomeInt( int value );
SomeInt( const SomeInt& other ); // copy
SomeInt( SomeInt&& other ) = default; // move
virtual ~SomeInt();
SomeInt& operator=( const SomeInt& other ); // assign
SomeInt& operator=( SomeInt&& other ) = default; // move assign
int getValue() const;
private:
class impl;
std::unique_ptr<impl> myImpl;
};
}
SomeInt.cpp
#include "SomeInt.h"
namespace pimplproblem
{
class SomeInt::impl
{
public:
impl( int value )
:myValue( value )
{}
int getValue() const
{
return myValue;
}
private:
int myValue;
};
SomeInt::SomeInt( int value )
:myImpl( new impl( value ) )
{}
SomeInt::SomeInt( const SomeInt& other )
:myImpl( new impl( other.getValue() ) )
{}
SomeInt::~SomeInt()
{}
SomeInt& SomeInt::operator=( const SomeInt& other )
{
myImpl = std::unique_ptr<impl>( new impl( other.getValue() ) );
return *this;
}
int SomeInt::getValue() const
{
return myImpl->getValue();
}
}
SomeComposite.h
#pragma once
#include <iostream>
#include <memory>
#include "SomeInt.h"
namespace pimplproblem
{
class SomeComposite
{
public:
explicit SomeComposite( const SomeInt& value );
SomeComposite( const SomeComposite& other ); // copy
SomeComposite( SomeComposite&& other ) = default; // move
virtual ~SomeComposite();
SomeComposite& operator=( const SomeComposite& other ); // assign
SomeComposite& operator=( SomeComposite&& other ) = default; // move assign
SomeInt getValue() const;
private:
class impl;
std::unique_ptr<impl> myImpl;
};
}
SomeComposite.cpp
#include "SomeComposite.h"
namespace pimplproblem
{
class SomeComposite::impl
{
public:
impl( const SomeInt& value )
:myValue( value )
{}
SomeInt getValue() const
{
return myValue;
}
private:
SomeInt myValue;
};
SomeComposite::SomeComposite( const SomeInt& value )
:myImpl( new impl( value ) )
{}
SomeComposite::SomeComposite( const SomeComposite& other )
:myImpl( new impl( other.getValue() ) )
{}
SomeComposite::~SomeComposite()
{}
SomeComposite& SomeComposite::operator=( const SomeComposite& other )
{
myImpl = std::unique_ptr<impl>( new impl( other.getValue() ) );
return *this;
}
SomeInt SomeComposite::getValue() const
{
return myImpl->getValue();
}
}
您不能使用 Pimpl 类 在头文件中声明的默认构造函数和赋值运算符(例如 SomeInt( SomeInt&& other ) = default;
),因为默认实现是内联的,并且在声明点 SomeInt
的声明 SomeInt::impl
不完整,所以 unique_ptr
抱怨。你必须自己声明和定义所有特殊的成员函数。
即改变SomeInt
和SomeComposite
声明如下:
// SomeInt.h
SomeInt( SomeInt&& other ); // move
SomeInt& operator=( SomeInt&& other ); // move assign
// SomeInt.cpp
// after definition of SomeInt::impl
SomeInt::SomeInt( SomeInt&& other ) = default;
SomeInt& operator=( SomeInt&& other ) = default;
另一种选择是创建您自己的 Pimpl 指针,如 this answer 中所建议。
对于演示该问题所需的大量代码,我深表歉意。我在使用带 std::unique_ptr 的 pimpl 习语时遇到问题。具体来说,当一个 class(具有 pimpl 的实现)在另一个具有 pimpl 的实现的复合 class 中用作成员数据时,问题似乎会发生。
我能找到的大多数答案都缺少 explicit destructor declaration,但正如您在这里看到的,我已经声明并定义了析构函数。
这段代码有什么问题,是否可以在不改变设计的情况下修改编译?
注意:错误似乎出现在 SomeComposite::getValue() 的定义中,并且编译器直到编译时才能看到错误。在 memory.h 中遇到错误,消息是 Invalid application of 'sizeof' to an incomplete type 'pimplproblem::SomeInt::impl'.
SomeInt.h
#pragma once
#include <iostream>
#include <memory>
namespace pimplproblem
{
class SomeInt
{
public:
explicit SomeInt( int value );
SomeInt( const SomeInt& other ); // copy
SomeInt( SomeInt&& other ) = default; // move
virtual ~SomeInt();
SomeInt& operator=( const SomeInt& other ); // assign
SomeInt& operator=( SomeInt&& other ) = default; // move assign
int getValue() const;
private:
class impl;
std::unique_ptr<impl> myImpl;
};
}
SomeInt.cpp
#include "SomeInt.h"
namespace pimplproblem
{
class SomeInt::impl
{
public:
impl( int value )
:myValue( value )
{}
int getValue() const
{
return myValue;
}
private:
int myValue;
};
SomeInt::SomeInt( int value )
:myImpl( new impl( value ) )
{}
SomeInt::SomeInt( const SomeInt& other )
:myImpl( new impl( other.getValue() ) )
{}
SomeInt::~SomeInt()
{}
SomeInt& SomeInt::operator=( const SomeInt& other )
{
myImpl = std::unique_ptr<impl>( new impl( other.getValue() ) );
return *this;
}
int SomeInt::getValue() const
{
return myImpl->getValue();
}
}
SomeComposite.h
#pragma once
#include <iostream>
#include <memory>
#include "SomeInt.h"
namespace pimplproblem
{
class SomeComposite
{
public:
explicit SomeComposite( const SomeInt& value );
SomeComposite( const SomeComposite& other ); // copy
SomeComposite( SomeComposite&& other ) = default; // move
virtual ~SomeComposite();
SomeComposite& operator=( const SomeComposite& other ); // assign
SomeComposite& operator=( SomeComposite&& other ) = default; // move assign
SomeInt getValue() const;
private:
class impl;
std::unique_ptr<impl> myImpl;
};
}
SomeComposite.cpp
#include "SomeComposite.h"
namespace pimplproblem
{
class SomeComposite::impl
{
public:
impl( const SomeInt& value )
:myValue( value )
{}
SomeInt getValue() const
{
return myValue;
}
private:
SomeInt myValue;
};
SomeComposite::SomeComposite( const SomeInt& value )
:myImpl( new impl( value ) )
{}
SomeComposite::SomeComposite( const SomeComposite& other )
:myImpl( new impl( other.getValue() ) )
{}
SomeComposite::~SomeComposite()
{}
SomeComposite& SomeComposite::operator=( const SomeComposite& other )
{
myImpl = std::unique_ptr<impl>( new impl( other.getValue() ) );
return *this;
}
SomeInt SomeComposite::getValue() const
{
return myImpl->getValue();
}
}
您不能使用 Pimpl 类 在头文件中声明的默认构造函数和赋值运算符(例如 SomeInt( SomeInt&& other ) = default;
),因为默认实现是内联的,并且在声明点 SomeInt
的声明 SomeInt::impl
不完整,所以 unique_ptr
抱怨。你必须自己声明和定义所有特殊的成员函数。
即改变SomeInt
和SomeComposite
声明如下:
// SomeInt.h
SomeInt( SomeInt&& other ); // move
SomeInt& operator=( SomeInt&& other ); // move assign
// SomeInt.cpp
// after definition of SomeInt::impl
SomeInt::SomeInt( SomeInt&& other ) = default;
SomeInt& operator=( SomeInt&& other ) = default;
另一种选择是创建您自己的 Pimpl 指针,如 this answer 中所建议。