有一条规则说明std :: tuple的成员被破坏了吗?
例如,如果Function1
返回一个std::tuple<std::unique_ptr<ClassA>, std::unique_ptr<ClassB>>
to Function2
,那么我可以确定(当Function2
剩下的范围时)ClassB
第二个成员ClassA
引用的实例在第一个成员引用的实例之前被销毁吗?
std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > Function1()
{
std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > garbage;
get<0>(garbage).reset( /* ... */ );
get<1>(garbage).reset( /* ... */ );
return garbage;
}
void Function2()
{
auto to_be_destroyed = Function1();
// ... do something else
// to_be_destroyed leaves scope
// Is the instance of ClassB destroyed before the instance of ClassA?
}
Run Code Online (Sandbox Code Playgroud) 我正在实现一个简单的智能指针,它基本上跟踪它处理的指针的引用数量.
我知道我可以实现移动语义,但我不认为复制智能指针非常便宜.特别是考虑到它引入了产生令人讨厌的错误的机会.
这是我的C++ 11代码(我省略了一些不必要的代码).欢迎提出一般性意见.
#ifndef SMART_PTR_H_
#define SMART_PTR_H_
#include <cstdint>
template<typename T>
class SmartPtr {
private:
struct Ptr {
T* p_;
uint64_t count_;
Ptr(T* p) : p_{p}, count_{1} {}
~Ptr() { delete p_; }
};
public:
SmartPtr(T* p) : ptr_{new Ptr{p}} {}
~SmartPtr();
SmartPtr(const SmartPtr<T>& rhs);
SmartPtr(SmartPtr<T>&& rhs) =delete;
SmartPtr<T>& operator=(const SmartPtr<T>& rhs);
SmartPtr<T>& operator=(SmartPtr<T>&& rhs) =delete;
T& operator*() { return *ptr_->p_; }
T* operator->() { return ptr_->p_; }
uint64_t Count() const { return ptr_->count_; }
const T* …
Run Code Online (Sandbox Code Playgroud) 这是怎么回事,标准没有提到once_flag的可移动性?我希望应用与 std::mutex 相同的参数。至少对于 gcc(4.8 版)来说,移动似乎被禁用了。如果某个编译器允许移动,那么最终可能会得到不可移植的代码。