【发布时间】:2018-06-25 00:33:08
【问题描述】:
我创建了一个通用删除器模板,可用于创建 unique_ptr<>() 子类型,允许 Deleter 而不仅仅是 delete ptr。
它适用于默认优化标志(即-O0),但是,当我使用-O3 时,T & operator * () 函数不知何故返回0 而不是f_pointer 内容。
我想确保我们同意编译器有问题并且我的模板是正确的。以下是一段完整的代码,应该在 Ubuntu 16.04 和 Ubuntu 18.04 以及其他支持 C++14 的版本下编译(请参阅下面的测试 g++ 版本)。
// RAII Generic Deleter -- allow for any type of RAII deleter
//
// To break compile with:
// g++ --std=c++14 -O3 -DNDEBUG ~/tmp/b.cpp -o b
#include <memory>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
template<class T, T null_value, class D, D deleter>
class raii_generic_deleter
{
public:
class pointer
{
private:
T f_pointer = null_value;
public:
pointer(T p)
: f_pointer(p)
{
}
pointer(std::nullptr_t = nullptr)
: f_pointer(null_value)
{
}
explicit operator bool () const
{
return f_pointer != null_value;
}
bool operator == (pointer const rhs) const
{
return f_pointer == rhs.f_pointer;
}
bool operator != (pointer const rhs) const
{
return f_pointer != rhs.f_pointer;
}
T & operator * ()
{
return f_pointer;
}
};
void operator () (pointer p)
{
deleter(*p);
}
};
typedef std::unique_ptr<int,
raii_generic_deleter<int, -1, decltype(&::close), &::close>>
raii_fd_t;
int main(int argc, char * argv [])
{
int fd = -1;
{
raii_fd_t safe_fd;
std::cout << "default initialization: safe_fd = " << *safe_fd
<< std::endl;
fd = open("/tmp/abc.tmp", O_RDWR | O_CREAT, 0700);
std::cout << "fd = " << fd << std::endl;
safe_fd.reset(fd);
std::cout << "safe_fd after the reset(" << fd
<< ") = " << *safe_fd << std::endl;
}
if(fd != -1)
{
// assuming the safe_fd worked as expected, this call returns an error
//
int r = close(fd);
int e(errno);
std::cout << "second close returned " << r
<< " (errno = " << e << ")" << std::endl;
}
return 0;
}
(原文见raii_generic_deleter.h libsnapwebsites)
当我使用-O0(没有优化)时,我得到了输出:
default initialization: safe_fd = -1
fd = 3
safe_fd after the reset(3) = 3
second close returned -1 (errno = 9)
在这种情况下,*safe_fd 调用按预期返回-1 和3。这会调用模板T & pointer::operator * () 函数。
使用任何级别的优化(-O1、-O2、-O3),输出如下所示:
default initialization: safe_fd = 0
fd = 3
safe_fd after the reset(3) = 0
second close returned -1 (errno = 9)
正如我们所见,安全文件描述符在初始化后返回0 而不是-1,然后在应该是3 时再次返回0。但是,析构函数会正确关闭文件,因为第二次关闭按预期失败。换句话说,不知何故,文件描述 (3) 是已知的并被删除器正确使用。
当我以这种方式更新指针运算符时:
T & operator * ()
{
std::cout << "f_pointer within operator * = " << f_pointer
<< std::endl;
return f_pointer;
}
那么任何优化级别的输出都是正确的:
f_pointer within operator * = -1
default initialization: safe_fd = -1
fd = 3
f_pointer within operator * = 3
safe_fd after the reset(3) = 3
f_pointer within operator * = 3
second close returned -1 (errno = 9)
这可能是因为该特定功能没有完全优化。
编译器:
我在 Ubuntu 16.04 上使用 stock g++ 进行了测试
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
在 Ubuntu 18.04 上也是如此
g++ (Ubuntu 7.3.0-16ubuntu3) 7.3.0
我也将此事报告为bug on the GNU website。
【问题讨论】:
-
这不能用 libc++ 编译。
-
@n.m.它没有编译器或链接?您在什么操作系统上尝试过?
-
由于 const 正确性而无法编译。在 Linux 和 cygwin 上试过。可以通过使
f_pointer可变和operator*成为返回非常量 ref 的 const 成员函数来修复(如果您考虑一下,这是合理的)。但这并不影响主要问题。 -
我的代码是基于这个stackoverflow.com/questions/15756960/…,它有一个非常量的
T & operator * ()函数,我还没有找到关于正确实现pointer类的好文档。 -
更正,值初始化似乎执行正常,但 opeeator* 看起来已损坏。
标签: c++ templates g++ ubuntu-16.04 unique-ptr