admin管理员组文章数量:1201202
I have a class that requires an a list of elements to initialize. I have a sub-class that whenever used, should always add elements to the initializer_list passed
through,
#include <vector>
#include <iostream>
class A
{
public:
A(std::initializer_list<int> things)
:
m_things(things)
{}
virtual ~A() {}
virtual void X() const noexcept
{
for(const auto& i : m_things)
{
std::cout << i << std::endl;
}
}
protected:
std::vector<int> m_things;
};
class B : public A
{
public:
B(std::initializer_list<int> things)
:
A(things /* how do I add some elements here? */)
{}
};
int main()
{
A* a = new B({1,2,4});
a->X();
return 0;
}
In addition, I might need to manipulate the passed values, in either A
or B
before putting them into either the m_things
or A
s constructor, how would I do that?
The current answer provides a way to accomplish this using std::views::concat
, however, I don't currently have access to GCC 15 (only currently supporting compiler for this C++26 feature). I'm currently using GCC 14.2.1
I have a class that requires an a list of elements to initialize. I have a sub-class that whenever used, should always add elements to the initializer_list passed
through,
#include <vector>
#include <iostream>
class A
{
public:
A(std::initializer_list<int> things)
:
m_things(things)
{}
virtual ~A() {}
virtual void X() const noexcept
{
for(const auto& i : m_things)
{
std::cout << i << std::endl;
}
}
protected:
std::vector<int> m_things;
};
class B : public A
{
public:
B(std::initializer_list<int> things)
:
A(things /* how do I add some elements here? */)
{}
};
int main()
{
A* a = new B({1,2,4});
a->X();
return 0;
}
In addition, I might need to manipulate the passed values, in either A
or B
before putting them into either the m_things
or A
s constructor, how would I do that?
The current answer provides a way to accomplish this using std::views::concat
, however, I don't currently have access to GCC 15 (only currently supporting compiler for this C++26 feature). I'm currently using GCC 14.2.1
1 Answer
Reset to default 4You can't do it with std::initialiser_list
, as that can't be modified, but you can use a different constructor.
template< class R, class T >
concept container_compatible_range =
std::ranges::input_range<R> &&
std::convertible_to<std::ranges::range_reference_t<R>, T>;
class A
{
public:
A(std::initializer_list<int> things)
:
m_things(things)
{}
A(std::from_range_t from_range, container_compatible_range<int> auto things)
:
m_things(from_range, things)
{}
virtual ~A() {}
virtual void X() const noexcept
{
for(const auto& i : m_things)
{
std::cout << i << std::endl;
}
}
protected:
std::vector<int> m_things;
};
class B : public A
{
static constexpr std::initializer_list<int> stuff = { 10, 11, 12 };
public:
B(std::initializer_list<int> things)
:
A(std::from_range, std::views::concat(things, stuff))
{}
};
int main()
{
A* a = new B({1,2,4});
a->X();
return 0;
}
本文标签:
版权声明:本文标题:c++ - How do I add elements to a initializer_list before it's passed to a class member at construction time? - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738563725a2099734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
A(std::initializer_list<int> things, std::initializer_list<int> more_things) : m_things(things) { m_things.insert(m_things.end(), more_things); }
– Eljay Commented Jan 22 at 12:58initializer_list
is immutable and contiguous, you can only create non empty ones with explicit{..}
syntax... – Jarod42 Commented Jan 22 at 13:05