使用值列表初始化模板数组

发布于 2024-10-25 04:55:14 字数 1025 浏览 2 评论 0原文

在标准 C++ 中,我们可以这样写:

int myArray[5] = {12, 54, 95, 1, 56};

我想用模板写同样的东西:

Array<int, 5> myArray = {12, 54, 95, 1, 56};

假设

template <class Type, unsigned long N>
class Array
{
public:

    //! Default constructor
    Array();

    //! Destructor
    virtual ~Array();

    //! Used to get the item count
    //! @return the item count
    unsigned long getCount() const;

    //! Used to access to a reference on a specified item
    //! @param the item of the item to access
    //! @return a reference on a specified item
    Type & operator[](const unsigned long p_knIndex);

    //! Used to access to a const reference on a specified item
    //! @param the item of the item to access
    //! @return a const reference on a specified item
    const Type & operator[](const unsigned long p_knIndex) const;

private:

    //! The array collection
    Type m_Array[N];
};

我认为这是不可能的,但可能有一个棘手的方法来做到这一点!

In standard c++ we can write :

int myArray[5] = {12, 54, 95, 1, 56};

I would like to write the same thing with a template :

Array<int, 5> myArray = {12, 54, 95, 1, 56};

assuming that

template <class Type, unsigned long N>
class Array
{
public:

    //! Default constructor
    Array();

    //! Destructor
    virtual ~Array();

    //! Used to get the item count
    //! @return the item count
    unsigned long getCount() const;

    //! Used to access to a reference on a specified item
    //! @param the item of the item to access
    //! @return a reference on a specified item
    Type & operator[](const unsigned long p_knIndex);

    //! Used to access to a const reference on a specified item
    //! @param the item of the item to access
    //! @return a const reference on a specified item
    const Type & operator[](const unsigned long p_knIndex) const;

private:

    //! The array collection
    Type m_Array[N];
};

I thinks it is not possible but may be there's a tricky way to do it !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

薄暮涼年 2024-11-01 04:55:14

我的解决方案是编写一个类模板来累积传递给构造函数的所有值。下面是您现在初始化 Array 的方法:

Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);

adder 的实现如下所示,并带有完整演示:

template<typename T>
struct adder
{
   std::vector<T> items;
   adder(const T &item) { items.push_back(item); }
   adder& operator,(const T & item) { items.push_back(item); return *this; }
};

template <class Type, size_t N>
class Array
{
public:

    Array(const adder<Type> & init) 
    {
         for ( size_t i = 0 ; i < N ; i++ )
         {
               if ( i < init.items.size() )
                   m_Array[i] = init.items[i];
         }
    }
    size_t Size() const { return N; }
    Type & operator[](size_t i) { return m_Array[i]; }
    const Type & operator[](size_t i) const { return m_Array[i]; }

private:

    Type m_Array[N];
};

int main() {

        Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

输出:

1
2
3
4
5
6
7
8
9
10

请自行查看 ideone 的在线演示:http://www.ideone.com/KEbTR

My solution is to write a class template that accumulates all the values which get passed to the constructor. Here is how you can initizalize your Array now:

Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);

The implementation of adder is shown below with complete demonstration:

template<typename T>
struct adder
{
   std::vector<T> items;
   adder(const T &item) { items.push_back(item); }
   adder& operator,(const T & item) { items.push_back(item); return *this; }
};

template <class Type, size_t N>
class Array
{
public:

    Array(const adder<Type> & init) 
    {
         for ( size_t i = 0 ; i < N ; i++ )
         {
               if ( i < init.items.size() )
                   m_Array[i] = init.items[i];
         }
    }
    size_t Size() const { return N; }
    Type & operator[](size_t i) { return m_Array[i]; }
    const Type & operator[](size_t i) const { return m_Array[i]; }

private:

    Type m_Array[N];
};

int main() {

        Array<int, 10> array = (adder<int>(1),2,3,4,5,6,7,8,9,10);
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

See the online demo at ideone yourself : http://www.ideone.com/KEbTR

尐籹人 2024-11-01 04:55:14

在 C++0x 中,使用初始化程序列表可以实现这一点。目前,还没有办法做到这一点。

没有这个你能得到的最接近的是使用 Boost.Assign

This becomes possible in C++0x using initializer lists. Currently, there is no way to do this.

The closest you can get without this is to use Boost.Assign.

英雄似剑 2024-11-01 04:55:14

这其实是一件很微不足道的事;只需删除构造函数并
公开数据成员。模板问题是红色的
赫林;相同的规则适用于任何类别:如果它是
聚合,可以使用聚合初始化;如果不是的话
你不能。

--
詹姆斯·坎泽

It's actually very trivial; just remove the constructors and
make the data members public. The template issue is a red
hering; the same rules apply as for any class: if it is an
aggregate, you can use aggregate initialization; if it's not,
you can't.

--
James Kanze

匿名的好友 2024-11-01 04:55:14

另一个不需要 adder 类模板的解决方案。现在您可以执行以下操作:

int main() {

        Array<int, 10> array;
        array = 1,2,3,4,5,6,7,8,9,10;
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

输出:

1
2
3
4
5
6
7
8
9
10

这是完整的解决方案: http://www.ideone.com/I0L1C

Yet another solution which doesn't need adder class template. Now you can do this:

int main() {

        Array<int, 10> array;
        array = 1,2,3,4,5,6,7,8,9,10;
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

Here is the complete solution: http://www.ideone.com/I0L1C

任谁 2024-11-01 04:55:14

你说得对。这对于当前的标准 C++ 来说是不可能的。然而,使用下一个标准(c++0x)初始化列表将做到这一点!

You're right. This is not possible with current standard C++. However, with the next standard (c++0x) initializer-lists will do just that!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文