Я хотел бы переместить элементы из переменной типа кортежа члена класса, но мне интересно, разрешен ли следующий код (почти минимальный пример, ограниченный C++14):
Код: Выделить всё
#include
#include
// For testing, assume a move-only class template:
enum class Index { idx1, idx2, idx3 };
template
struct B {
~B() = default;
B(B&&) = default;
B& operator= (B&&) = default;
B(B const&) = delete;
B& operator= (B const&) = delete;
Index getIndex() { return I; }
};
// Class to consider
template
class A
{
public:
A(B&&... _b) : m_tuple{std::move(_b)...} {}
template
std::tuple moveBs() &&
{
return { std::move(*this).template moveB()... };
}
private:
template
B moveB() &&
{
// Attention: The entire tuple shall be moved, not the return
// value(s) of std::get() - see answer to question
// 77664807: "How does std::forward work in the
// context of a fold expression?", thanks to
// 'user12002570' for pointing this out.
return std::get(std::move(m_tuple));
}
std::tuple m_tuple;
};
// Output only as an example, to try out
void print(Index _i)
{
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/77672833/how-can-i-move-stdtuple-elements-out-of-a-class-object[/url]
Мобильная версия