如何在C++中使用花括号整数列表初始化构造函数?

huangapple go评论74阅读模式
英文:

How to use braced integer list initialization for constructor in c++?

问题

我正在尝试但无法为我的类创建一个构造函数,该构造函数接受整数参数(以后可能更改为某种自定义数据类型),通过用大括号初始化列表,就像经典的旧int a[]{0,1,2}一样。以下是我的代码:

#include <iostream>
using namespace std;

class class_a {
    private:
        int ptr;
    public:
        class_a() {};
        template <typename ... Args>
        class_a(Args&& ... args) : ptr((args)...) {}
};

int main()
{
    class_a c1{0}; //工作正常
    //class_a c2[]{0,1,2}; //不起作用
}

我希望输出的变量ptr可以初始化为整数数组{0,1,2}(只是暂时的 - 以后可能更改为任何复杂的数据类型)。

英文:

I am trying but unable to create a constructor for my class that takes in integer arguments (which may change to some custom data type later) through braced list initialization like the classic old int a[]{0,1,2}. Here is my code

#include&lt;iostream&gt;
using namespace std;

class class_a {
    private:
        int ptr;
    public:
        class_a() {};
        template&lt;typename ... Args&gt;
        class_a(Args&amp;&amp; ... args) : ptr((args)...) {}
};

int main()
{
    class_a c1{0}; //works
    //class_a c2[]{0,1,2}; //doesnt works
}

I want an output where the variable ptr can be initialized as the array of integers {0,1,2} (again, just for now - it may change to any complex data type later)

答案1

得分: 2

由于这个类不是一个类模板(或者说没有指定“ptr”变量应该包含多少元素),我假设你真正需要的是一个std::vector<int>,它可以按照你想要的方式进行初始化:

#include <utility>
#include <vector>

class class_a {
private:
    std::vector<int> ptr;

public:
    class_a() = default;

    template <typename... Args>
    class_a(Args&&... args) : ptr{std::forward<Args>(args)...} {}
    // 使用完美转发以便以后更改为用户定义的类型
};
int main() {
    class_a c1{0};        // 可行
    class_a c2{0, 1, 2};  // 现在可行
}

如果你真的希望ptr是一个数组,那么你需要将class_a变成一个类模板:

template<size_t N>
class class_a {
private:
    int ptr[N];            // 现在是一个数组

public:
    class_a() = default;

    template <typename... Args>
    class_a(Args&&... args) : ptr{std::forward<Args>(args)...} {}
};

// 类型推导指南
template<class... Args> class_a(Args&&...) -> class_a<sizeof...(Args)>;

但请注意,具有不同N的class_a<N>现在是不同的类型,与使用std::vector<int>作为成员变量不同。

英文:

Since the class is not a class template (or something saying how many elements there should be in the "ptr" variable) I'm assuming that what you need is really a std::vector&lt;int&gt;, which could be initialized the way you want:

#include &lt;utility&gt;
#include &lt;vector&gt;

class class_a {
private:
    std::vector&lt;int&gt; ptr;

public:
    class_a() = default;

    template &lt;typename... Args&gt;
    class_a(Args&amp;&amp;... args) : ptr{std::forward&lt;Args&gt;(args)...} {}
    // with perfect forwarding in case you change to a user-defined type later
};
int main() {
    class_a c1{0};        // works
    class_a c2{0, 1, 2};  // now works
}

If you really want ptr to be an array, you need to make class_a a class template:

template&lt;std::size_t N&gt;
class class_a {
private:
    int ptr[N];            // now an array

public:
    class_a() = default;

    template &lt;typename... Args&gt;
    class_a(Args&amp;&amp;... args) : ptr{std::forward&lt;Args&gt;(args)...} {}
};

// deduction guide
template&lt;class... Args&gt; class_a(Args&amp;&amp;...) -&gt; class_a&lt;sizeof...(Args)&gt;;

But note that class_a&lt;N&gt;s with different Ns are now different types unlike if you use a std::vector&lt;int&gt; as a member variable.

huangapple
  • 本文由 发表于 2023年2月14日 00:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定