gpt4 book ai didi

c++ - std::tuple 元素的延迟初始化

转载 作者:行者123 更新时间:2023-11-28 01:26:35 25 4
gpt4 key购买 nike

我经常使用 std::aligned_storage指定一个未初始化的类成员。典型示例是 static_vector,它将其元素存储在结构中。

但是,我不完全确定当我想要 std::tuple 时我应该做什么逐步创建,在不同的时间点以未指定的顺序初始化其成员。

创建是否合法

std::tuple< std::aligned_storage<sizeof(Types),alignof(Types)>::type...>

然后将成员引用重新解释为 std::tuple<Types...>&

例如:

#include <bitset>
#include <memory>
#include <new>
#include <tuple>
#include <utility>

template < class... Ts >
class uninitialized_tuple {
public:
using tuple_type = typename std::tuple<Ts...>;
using buffer_type =
std::tuple<
typename std::aligned_storage<sizeof(Ts),alignof(Ts)>::type...
>;

~uninitialized_tuple() {
destruct_helper<std::index_sequence_for<Ts...>>::erase(*this);
}

tuple_type& as_tuple() {
reinterpret_cast<tuple_type&>(_storage);
}

bool valid() const {
return _is_set.all();
}

template < size_t index, class... Args >
void emplace( Args&&... args ) {
using element_type = typename std::tuple_element<index,tuple_type>::type;
new (&std::get<index>(_storage)) element_type( std::forward<Args>(args)...);
_is_set.set(index);
}

template < size_t index >
void erase() {
using element_type = typename std::tuple_element<index,tuple_type>::type;
if( _is_set[index] ) {
std::get<index>(_storage).~element_type();
_is_set.reset(index);
}
}

private:
template < class Seq >
struct destruct_helper {
static void erase( uninitialized_tuple& ) {}
};

template < size_t index, size_t... indices >
struct destruct_helper<std::index_sequence<index,indices...>> {
static void erase( uninitialized_tuple& value ) {
value.erase<index>();
destruct_helper<std::index_sequence<indices...>>::erase_one(value);
}
};

buffer_type _storage;
std::bitset<sizeof...(Ts)> _is_set;
};

最佳答案

访问 as_tuple() 返回的任何内容是未定义的行为,因为它违反了类型别名规则。请引用https://en.cppreference.com/w/cpp/language/reinterpret_cast :

Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a glvalue of type AliasedType, the behavior is undefined unless one of the following is true:

  • AliasedType and DynamicType are similar.

  • AliasedType is the (possibly cv-qualified) signed or unsigned variant of DynamicType.

  • AliasedType is std::byte, (since C++17)char, or unsigned char: this permits examination of the object representation of any object as an array of bytes.

关于c++ - std::tuple 元素的延迟初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com