使用值列表初始化模板数组
在标准 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的解决方案是编写一个类模板来累积传递给构造函数的所有值。下面是您现在初始化
Array
的方法:adder
的实现如下所示,并带有完整演示:输出:
请自行查看 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:The implementation of
adder
is shown below with complete demonstration:Output:
See the online demo at ideone yourself : http://www.ideone.com/KEbTR
在 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.
这其实是一件很微不足道的事;只需删除构造函数并
公开数据成员。模板问题是红色的
赫林;相同的规则适用于任何类别:如果它是
聚合,可以使用聚合初始化;如果不是的话
你不能。
--
詹姆斯·坎泽
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
另一个不需要
adder
类模板的解决方案。现在您可以执行以下操作:输出:
这是完整的解决方案: http://www.ideone.com/I0L1C
Yet another solution which doesn't need
adder
class template. Now you can do this:Output:
Here is the complete solution: http://www.ideone.com/I0L1C
你说得对。这对于当前的标准 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!