gpt4 book ai didi

c++ - 使用初始化列表初始化 unique_ptr 的容器,续

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:44 28 4
gpt4 key购买 nike

我想初始化 std::vector的和std::mapstd::unique_ptr<T>的。就我而言,T是固定的。在这个例子中,我假设 T = int ,尽管实际上它是一个基类,从中派生出许多其他类。

我一直在研究给出的答案:

https://stackoverflow.com/a/46771893/4875652

而且我还没有设法让它为 std::map 工作案例。

以下作品:

#include <memory>
#include <vector>

struct movable_il {
mutable iptr t;
operator iptr() const&& { return std::move(t); }
movable_il( iptr&& in ): t(std::move(in)) {}
};

std::vector<iptr> vector_from_il( std::initializer_list<movable_il> il ) {
return std::vector<iptr>( std::make_move_iterator(il.begin()), std::make_move_iterator(il.end()) );
}

int main()
{
auto lol = vector_from_il({iptr{new int{3}}});

return 0;
}

但是,以下内容不会:

#include <memory>
#include <map>
#include <utility>

using iptr = std::unique_ptr<int>;

struct movable_il {
mutable std::pair<std::string, iptr> t;
operator std::pair<std::string, iptr>() const&& { return std::move(t); }
movable_il( std::pair<std::string, iptr>&& in ): t(std::move(in)) {}
};

std::map<std::string, iptr> container_from_il( std::initializer_list< movable_il> il ) {
return std::map<std::string, iptr>( std::make_move_iterator(il.begin()), std::make_move_iterator(il.end()) );
}

int main()
{
auto lol = container_from_il({std::pair<std::string, iptr>{"a", iptr{new int{3}}}});

return 0;
}

有什么想法吗?

更新:

我设法得到的最简单的工作示例,它与原始代码尽可能相似,没有使用模板,是这样的:

#include <iostream>
#include <map>
#include <memory>
#include <utility>

struct movable_pair {
using first_type = std::string;
using second_type = std::unique_ptr<int>;
using pair_type = std::pair<const first_type, second_type>;
first_type first;
mutable second_type second;
operator pair_type() const && { return {first, std::move(second)}; }
movable_pair(pair_type &&in): first{in.first}, second{std::move(in.second)} {}
};

auto map_from_il(std::initializer_list<movable_pair> il) {
return std::map<std::string, std::unique_ptr<int>>(std::make_move_iterator(il.begin()),
std::make_move_iterator(il.end()));
}

// Main function
int main() {
using iptr = std::unique_ptr<int>;
auto lol = map_from_il({{{"a", iptr{new int{3}}}}, {{"b", iptr{new int{2}}}}});

// Small print-out to check we inserted the correct elements :)
for (auto &l : lol) {
std::cout << l.first << " " << *l.second << std::endl;
}

return 0;
}

感谢用户 Banan 的帮助,特别是找出所需的额外一对花括号。

最佳答案

据我所知,主要问题是您需要使 可移动。这是由 movable_il 类完美完成的,但是现在您在尝试从这些构造 map 时遇到麻烦,因为 firstsecond 成员没有为 movable_il 定义。

因为您无论如何都在尝试从迭代器构造 map,所以我认为您自己进行迭代并手动插入元素不会对性能造成任何影响。我已经基于此制定了解决方案。这不是最漂亮的解决方案,但它可以完成工作。

此外,为了让编译器从“初始化列表”中推断出T的正确类型,我遇到了一些问题,因此我制作了一个小的辅助函数movable_pair 来创建这些,在这种情况下编译器没有问题。

#include <memory>
#include <map>
#include <utility>
#include <iostream>
#include <vector>

// Wrapper class to make type T "movable"
template<class T>
struct movable_il
{
mutable T t;
operator T() const&& { return std::move(t); }
movable_il( T&& in ): t(std::move(in)) {}

T get() const { return std::move(t); }
};

// Some template magic to deduce the correct value_type
// ( not really needed for this example as we are mostly interested in maps )
template<class VT>
struct fix_vt
{
using type = VT;
};

template<class VT>
using fix_vt_t = typename fix_vt<VT>::type;

template<class VT> struct fix_vt<const VT> : fix_vt<VT> {};

template<class K, class V>
struct fix_vt< std::pair<K,V> >
{
using type = std::pair<
typename std::remove_cv<K>::type,
typename std::remove_cv<V>::type
>;
};

// Create map from initializer list of movable T (pairs)
template<class C, class T = fix_vt_t<typename C::value_type> >
auto map_from_il(std::initializer_list< movable_il<T> > il)
{
using map_type = C;
auto map = map_type{};

// Loop over il list and insert each element into the map
for(auto&& entry : il)
{
map.insert(std::move(entry.get())); // We get the pair from the movable class and insert it by moving
}

return map;
}

// Helper function to create movable pair
template<class First, class Second>
auto movable_pair(First&& f, Second&& s)
{
using pair_type = std::pair<First, Second>;
return movable_il<pair_type>{ pair_type{ std::forward<First>(f), std::forward<Second>(s) } };
}

// Main function
int main()
{
using iptr = std::unique_ptr<int>;
using key_type = std::string;
using value_type = iptr;

using map_type = std::map<key_type, value_type>;

auto lol = map_from_il<map_type>({ movable_pair("a", iptr{ new int {3} } ), movable_pair("b", iptr{ new int {2} }) });

// Small print-out to check we inserted the correct elements :)
for(auto& l : lol)
{
std::cout << l.first << " " << *l.second << std::endl;
}

return 0;
}

免责声明:我只使用 GCC 8.1.0 对此进行了测试(但我认为使用其他编译器不会有任何问题)。

更新:如果您在将对传递给 map_from_il 时输入额外的一组 {} ,代码编译时不使用 movable_pair 辅助函数。

auto lol = map_from_il<map_type>({ { {"a", iptr{ new int {3} } } }, { {"b", iptr{ new int {2} } } } });

更新 2: 如果您将以下构造函数添加到 movable_il,代码也可以在没有额外的 {} 的情况下编译:

template<class... U>
movable_il( U&&... in): t{std::forward<U>(in)...} {}

有了这个你可以写:

auto lol = map_from_il<map_type>({ {"a", iptr{ new int {3} } }, {"b", iptr{ new int {2} } } });

关于c++ - 使用初始化列表初始化 unique_ptr 的容器,续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50629016/

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