我想用特殊的析构函数重新定义unique_ptr
。因此,我尝试使用以下代码来模仿unique_ptr
的一些构造函数。不幸的是constexpr
构造函数拒绝构建,我不知道为什么。
class job_ptr : public unique_ptr<Job>
{
public:
constexpr job_ptr()
: unique_ptr<Job>(), sequencer( nullptr ) {}
constexpr job_ptr( nullptr_t )
: unique_ptr<Job>( nullptr ), sequencer( nullptr ) {}
private:
FIFOSequencer* sequencer;
};
初始化列表中的两个构造函数都声明为constexpr
,但clang++
认为constexpr constructor never produces a constant expression
因为non-literal type 'unique_ptr<Job>' cannot be used in a constant expression
。这意味着什么? constexpr
构造函数不能在constexpr
构造函数中使用?
感谢您的帮助。
答案 0 :(得分:5)
Constexpr构造函数是可能的,但是the requirement are quite strict。正如@dyp所说,你的主要问题是std::unique_ptr
不是一个简单的析构函数,因此不是LiteralType
。
如果你在g ++下试用int
:
class int_ptr : public std::unique_ptr<int>
{
public:
constexpr int_ptr()
: std::unique_ptr<int>(), sequencer( nullptr ) {}
constexpr int_ptr( nullptr_t )
: std::unique_ptr<int>( nullptr ), sequencer( nullptr ) {}
private:
int* sequencer;
};
constexpr int_ptr ptr;
您有一条非常明确的错误消息:
unique_ptr.cpp:40:20: error: the type ‘const int_ptr’ of constexpr variable ‘ptr’ is not literal
constexpr int_ptr ptr;
^
unique_ptr.cpp:27:7: note: ‘int_ptr’ is not literal because:
class int_ptr : public std::unique_ptr<int>
^
unique_ptr.cpp:27:7: note: ‘int_ptr’ has a non-trivial destructor
在您的情况下,正如评论中所建议的那样,使用自定义删除器。 STL容器不太适合继承。
这是自定义删除器的示例:
#include <memory>
#include <iostream>
template <typename T>
struct Deleter
{
void operator()(T* t)
{
std::cout << "Deleter::oerator(): " << t << std::endl;
delete t;
}
};
struct A
{
A()
{
std::cout << "A::A()" << std::endl;
}
~A()
{
std::cout << "A::~A()" << std::endl;
}
};
int main(int argc, char const *argv[])
{
std::unique_ptr<A, Deleter<A>> ptr(new A);
return 0;
}
输出:
A::A()
Deleter::oerator(): 0x600010480
A::~A()