有什么方法可以用C ++中的模板类正确实现PIMPL习惯用法?
例如,我有以下PIMPL类:
template <typename T> struct PrivateImplementation {
PrivateImplementation<T>(T* impl) : implementation(impl) {
}
~PrivateImplementation<T>() {
if (implementation != nullptr) delete implementation;
}
PrivateImplementation<T>(const PrivateImplementation<T>& c) = delete;
void operator=(const PrivateImplementation<T>& a) = delete;
PrivateImplementation<T>(PrivateImplementation<T>&& m) {
implementation = m.implementation;
m.implementation = nullptr;
}
T* operator->() const {
return implementation;
}
private:
T* implementation;
};
但是,当我像下面那样使用它时,编译器会抱怨(警告)我正在删除不完整的类型:
记录器
class LoggerStream {
public:
LoggerStream(std::wstring componentName);
~LoggerStream();
LoggerStream(const LoggerStream&) = delete;
void operator=(const LoggerStream&) = delete;
LoggerStream(LoggerStream&&);
void Write(std::wstring message, const char* __function__, const char* __file__, int __line__) const;
void Write(std::wstring message) const;
private:
struct LoggerStreamImpl;
PrivateImplementation<LoggerStreamImpl> impl;
};
记录器
struct LoggerStream::LoggerStreamImpl {
LoggerStreamImpl(std::wstring componentName) : componentName(componentName) { }
~LoggerStreamImpl() = default;
LoggerStreamImpl(const LoggerStreamImpl&) = delete;
void operator=(const LoggerStreamImpl&) = delete;
LoggerStreamImpl(LoggerStreamImpl&&);
const std::wstring componentName;
};
LoggerStream::LoggerStream(std::wstring componentName)
: impl(new LoggerStreamImpl { componentName }) { }
LoggerStream::~LoggerStream() { }
void LoggerStream::Write(std::wstring message, const char* __function__, const char* __file__, int __line__) const {
// Some implementation
}
void LoggerStream::Write(std::wstring message) const {
// Some implementation
}
我显然在.cpp中为
LoggerStreamImpl
定义了一个析构函数,那有什么用呢?谢谢。
最佳答案
当我使用不希望完全可见的类型存储std :: unique_ptr时(如pimpl),我使用自定义删除器,其中operator()在cpp中定义,而完整类可见。
它可以解决问题,并且通过链接时间优化,即使编译器认为相关,也可以优化引入的函数调用间接调用。
关于c++ - C4150:删除不完整类型和PIMPL惯用语的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21391061/