I've seen a code snippet here on the board that I don't understand:
struct expression_sequence
{
template<typename... Ts>
expression_sequence(Ts&&... ts) { }
};
template<typename T, class Tuple = std::vector<T>>
class vector
{
public:
template<typename... Elements>
vector(Elements&&... elements)
{
m_elements.reserve(sizeof...(Elements));
expression_sequence{ (m_elements.push_back(std::forward<Elements>(elements)), 0)... };
}
private:
Tuple m_elements;
};
What exactly is going on at
expression_sequence{ (m_elements.push_back(std::forward<Elements>(elements)), 0)... };
and why is it working?
I don't understand why we need to surround m_elements.push_back(std::forward<Elements>(elements))
by (
and , 0)
. (Why 0
, i.e. why an int
?). And what's the type of (m_elements.push_back(std::forward<Elements>(elements)), 0)
?