向量>使用派生的初始化列表
我对此有一个后续问题:
vector< simolor_ptr; simolor_ptr< a> >使用初始化列表
我想实现与引用问题中相同的目标,但是这次使用多态类别。也就是说,我想使用初始化列表创建一个std :: vector< std :: simel_ptr> base>
。
#include <vector>
#include <memory>
#include <iterator>
#include <iostream>
template<class T>
struct movable_il {
mutable T t;
operator T() const&& { return std::move(t); }
movable_il( T&& in ): t(std::move(in)) {}
};
template<class T, class A=std::allocator<T>>
std::vector<T,A> vector_from_il( std::initializer_list< movable_il<T> > il ) {
std::vector<T,A> r( std::make_move_iterator(il.begin()), std::make_move_iterator(il.end()) );
return r;
}
// Example with class hierarchy
class Base
{
public:
Base(int in) : x(in) {}
virtual ~Base() = default;
virtual void print() const = 0;
protected:
int x;
};
class Derived1 : public Base
{
public:
Derived1(int in) : Base(in) {}
virtual ~Derived1() = default;
virtual void print() const override { std::cout << "Derived1: " << x << std::endl; }
};
class Derived2 : public Base
{
public:
Derived2(int in) : Base(in) {}
virtual ~Derived2() = default;
virtual void print() const override { std::cout << "Derived2: " << x << std::endl; }
};
int main()
{
{
auto v = vector_from_il< std::unique_ptr<Base> >({
std::make_unique<Derived1>(7),
std::make_unique<Derived2>(3)
});
for (const auto & val : v)
val->print();
}
return 0;
}
编译器(ubuntu上的GCC 7.4.0 18)错误消息是:
template argument deduction/substitution failed:
cannot convert '{std::make_unique(_Args&& ...)
[with _Tp = Derived1;
_Args = {int};
typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Derived1, std::default_delete<Derived1> >](),
std::make_unique(_Args&& ...)
[with _Tp = Derived2;
_Args = {int};
typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Derived2, std::default_delete<Derived2> >]()}'
(type '<brace-enclosed initializer list>') to type 'std::initializer_list<movable_il<std::unique_ptr<Base> > >'
我假设错误与在模板参数扣除期间未考虑转换的事实有关,因此也许这是不可能的。
I have a follow-up question to this one:
vector<unique_ptr<A> > using initialization list
I would like to achieve essentially the same goal as in the referenced question, but this time using polymorphic classes. That is, I would like to create a std::vector<std::unique_ptr<Base>>
using an initialization list.
#include <vector>
#include <memory>
#include <iterator>
#include <iostream>
template<class T>
struct movable_il {
mutable T t;
operator T() const&& { return std::move(t); }
movable_il( T&& in ): t(std::move(in)) {}
};
template<class T, class A=std::allocator<T>>
std::vector<T,A> vector_from_il( std::initializer_list< movable_il<T> > il ) {
std::vector<T,A> r( std::make_move_iterator(il.begin()), std::make_move_iterator(il.end()) );
return r;
}
// Example with class hierarchy
class Base
{
public:
Base(int in) : x(in) {}
virtual ~Base() = default;
virtual void print() const = 0;
protected:
int x;
};
class Derived1 : public Base
{
public:
Derived1(int in) : Base(in) {}
virtual ~Derived1() = default;
virtual void print() const override { std::cout << "Derived1: " << x << std::endl; }
};
class Derived2 : public Base
{
public:
Derived2(int in) : Base(in) {}
virtual ~Derived2() = default;
virtual void print() const override { std::cout << "Derived2: " << x << std::endl; }
};
int main()
{
{
auto v = vector_from_il< std::unique_ptr<Base> >({
std::make_unique<Derived1>(7),
std::make_unique<Derived2>(3)
});
for (const auto & val : v)
val->print();
}
return 0;
}
the compiler (GCC 7.4.0 on Ubuntu 18) error message is:
template argument deduction/substitution failed:
cannot convert '{std::make_unique(_Args&& ...)
[with _Tp = Derived1;
_Args = {int};
typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Derived1, std::default_delete<Derived1> >](),
std::make_unique(_Args&& ...)
[with _Tp = Derived2;
_Args = {int};
typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Derived2, std::default_delete<Derived2> >]()}'
(type '<brace-enclosed initializer list>') to type 'std::initializer_list<movable_il<std::unique_ptr<Base> > >'
I assume the error is related to the fact that no conversions are taken into account during template argument deduction, so maybe this is just not possible to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加额外的构造函数
修复汇编。
demo 。
Adding extra constructor
fixes compilation.
Demo.