vendredi 11 août 2023

Can we use int... as an argument for template parameter pack directly?

Here is my problem:

template <class... Ts>
struct iniClass
{
private:
    std::initializer_list<int> m_data;
    int x;

public:
    iniClass(Ts... vars)
        : m_data({vars...}), x(8)
    {
        std::cout << "created object with first constructor\n";
        prin();
    }
    void prin()
    {
        for (auto var : m_data)
        {
            std::cout << var << ", ";
        }
        std::cout << '\n';
    }
};

int main()
{
    iniClass<std::initializer_list<int>> il1({1, 2, 3, 4}); // Work like a charm!
    iniClass<int...> il2(1, 2, 3, 4);                       // Didn't work, How to make this work?
}

Can we use int... (,and generally primativeTYPE...) as an argument for template parameter pack directly?
Is there a way to make it work in that way?

Aucun commentaire:

Enregistrer un commentaire