首页 > 解决方案 > 从可变模板参数创建一个 std::tuple

问题描述

std::tuple我遇到了一些问题,涉及与从可变参数模板类创建关联的移动语义。这是我的课(它使用的是 Qt,但这无关紧要):

template<typename ...Ts>
class QuestionBox : public QGroupBox
{

public:
    QuestionBox(const QString& question, Ts... args)
    {
        this->setTitle(question);
        layout = new QGridLayout(this);
        questionBoxElements = std::make_tuple<Ts...>(args...); //ERROR: Cannot bind rvalue ref QLabel*&& to lvalue QLabel* 

        auto loop = [&](auto& arg)
        {
            layout->addWidget(arg);
        };

        (loop(args), ...);
    }

private:
    QGridLayout* layout;
    std::tuple<Ts...> questionBoxElements;
};

它被称为:

auto divisionSlider = new FancySlider(0, 30);
auto divisionLabel1 = new QLabel("Ranking Duration (days) : ");
auto divisionLabel2 = new QLabel("Submission Duration (days) : ");

auto competitionDivison = new QuestionBox<FancySlider*, QLabel*, QLabel*>("How will the competition be divide up?", divisionSlider, divisionLabel1, divisionLabel2);

所以,这里的一切都工作正常,除了标记为抛出错误的行。我在这里要做的就是创建一个提供的类型和提供的参数的元组。

我尝试将导致错误的行替换为:

questionBoxElements = std::make_tuple<Ts...>(std::move(args...)); 

这会导致以前的错误消失,但是,然后我收到一个新错误(对于某些并非所有类型):

No matching function for call to 'move(Type1*&, Type2*&)'

我假设这是因为引发此错误的类型没有实现移动构造函数?那是对的吗?

标签: c++metaprogrammingmove

解决方案


推荐阅读