在c ++中初始化参数化类型的数组

时间:2013-05-09 20:38:04

标签: c++ arrays templates c++11 valgrind

我正在研究一种泛型类型,我需要一个堆栈分配的参数类型数组。它应该尽可能少的开销。这是考试的一部分,但现在它激起了我的好奇心。我试着环顾四周,但我找不到任何有用的东西。

我正在尝试创建一个数组并初始化成员,但我不完全确定如何正确地执行它。我有以下代码:

T b[_dim]; // typeof(_dim) == size_t

现在T可以是具有标准算术运算符的任何东西,默认构造函数。 当T是双精度时,我使用:

初始化数组
memset(b, 0, _dim*sizeof(T));

哪种类型适用于双打等。现在问题是T可能是理性的,例如:

方法1

struct Rational
{
   int numerator;  
   int denominator;
   Rational(): numerator(0), denominator(1) {}
};

如果在将所有内容都设置为0后将其转换为double,则会导致问题。 另一种方法是运行所有这些元素:

方法2

for(size_t i = 0; i < _dim; i++)
{
    b[i] = T();
}

正确初始化成员的有效方法是什么?

一个副节点是当我既不是理性的另一种类型。例如双。该程序运行正常,但valgrind抱怨以下错误消息(其中很多,反复):

==24450== Conditional jump or move depends on uninitialised value(s)
==24450==    at 0x5878B60: __printf_fp (printf_fp.c:731)
==24450==    by 0x5876B4B: vfprintf (vfprintf.c:1654)
==24450==    by 0x589B654: vsnprintf (vsnprintf.c:119)
==24450==    by 0x53938FD: std::__convert_from_v(__locale_struct* const&, char*, int, char const*, ...) (c++locale.h:93)
==24450==    by 0x53A118C: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, char, double) const (locale_facets.tcc:997)
==24450==    by 0x53A141F: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, double) const (locale_facets.tcc:1144)
==24450==    by 0x53A5966: std::ostream& std::ostream::_M_insert<double>(double) (locale_facets.h:2398)
==24450==    by 0x433E9D: void boost::io::detail::put_last<char, std::char_traits<char>, double>(std::basic_ostream<char, std::char_traits<char> >&, double&) (feed_args.hpp:115)
==24450==    by 0x43273E: void boost::io::detail::put<char, std::char_traits<char>, std::allocator<char>, double&>(double&, boost::io::detail::format_item<char, std::char_traits<char>, std::allocator<char> > const&, boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::string_type&, boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::internal_streambuf_t&, std::locale*) (feed_args.hpp:176)
==24450==    by 0x43141F: void boost::io::detail::distribute<char, std::char_traits<char>, std::allocator<char>, double&>(boost::basic_format<char, std::char_traits<char>, std::allocator<char> >&, double&) (feed_args.hpp:253)
==24450==    by 0x42F22F: boost::basic_format<char, std::char_traits<char>, std::allocator<char> >& boost::io::detail::feed<char, std::char_traits<char>, std::allocator<char>, double&>(boost::basic_format<char, std::char_traits<char>, std::allocator<char> >&, double&) (feed_args.hpp:263)
==24450==    by 0x42DB1E: boost::basic_format<char, std::char_traits<char>, std::allocator<char> >& boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::operator%<double>(double&) (format_class.hpp:68)
==24450== 
==24450== Use of uninitialised value of size 8
==24450==    at 0x5878B6A: __printf_fp (printf_fp.c:731)
==24450==    by 0x5876B4B: vfprintf (vfprintf.c:1654)
==24450==    by 0x589B654: vsnprintf (vsnprintf.c:119)
==24450==    by 0x53938FD: std::__convert_from_v(__locale_struct* const&, char*, int, char const*, ...) (c++locale.h:93)
==24450==    by 0x53A118C: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, char, double) const (locale_facets.tcc:997)
==24450==    by 0x53A141F: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, double) const (locale_facets.tcc:1144)
==24450==    by 0x53A5966: std::ostream& std::ostream::_M_insert<double>(double) (locale_facets.h:2398)
==24450==    by 0x433E9D: void boost::io::detail::put_last<char, std::char_traits<char>, double>(std::basic_ostream<char, std::char_traits<char> >&, double&) (feed_args.hpp:115)
==24450==    by 0x43273E: void boost::io::detail::put<char, std::char_traits<char>, std::allocator<char>, double&>(double&, boost::io::detail::format_item<char, std::char_traits<char>, std::allocator<char> > const&, boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::string_type&, boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::internal_streambuf_t&, std::locale*) (feed_args.hpp:176)
==24450==    by 0x43141F: void boost::io::detail::distribute<char, std::char_traits<char>, std::allocator<char>, double&>(boost::basic_format<char, std::char_traits<char>, std::allocator<char> >&, double&) (feed_args.hpp:253)
==24450==    by 0x42F22F: boost::basic_format<char, std::char_traits<char>, std::allocator<char> >& boost::io::detail::feed<char, std::char_traits<char>, std::allocator<char>, double&>(boost::basic_format<char, std::char_traits<char>, std::allocator<char> >&, double&) (feed_args.hpp:263)
==24450==    by 0x42DB1E: boost::basic_format<char, std::char_traits<char>, std::allocator<char> >& boost::basic_format<char, std::char_traits<char>, std::allocator<char> >::operator%<double>(double&) (format_class.hpp:68)

做我的两种方法中的任何一种使得valgrind停止抱怨。

更新变量_dim确实是变量,并在解析中.g ++ 11说:

error: variable-sized object ‘b’ may not be initialized

解决方案和摘要 正如所指出的,这称为所有元素的ctor:

b T[_dim];

由于_dim是变量,因此不起作用:

b T[_dim] = {};

但我们可以使用这个::

T b[_dim];
if(std::is_fundamental<T>::value)
{
    memset(b, 0, _dim*sizeof(T));
}

2 个答案:

答案 0 :(得分:2)

正如stfrabbit已经指出的那样,在定义数组时将为您调用默认的ctor。对于以下代码:

#include <iostream>
struct Rational
{
   int numerator;  
   int denominator;
   Rational(): numerator(0), denominator(1)
   {std::cout << "Rational ctor!" << std::endl;}
};

int main()
{
    Rational arr[10];
}

输出结果为:

Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!
Rational ctor!

因此,对于类类型,您没有什么特别的。但是,对于内置类型,没有默认的ctor,因此您需要添加几个大括号,即T b[_dim] = {};来初始化每个元素的值。通过这样做,它将适用于类类型和内置类型。

答案 1 :(得分:1)

你可以定义覆盖函数来初始化不同的T,我假设你正在使用支持覆盖的c ++。

void init(double& t)
{
  t = 0;
}
void init(Rational& t)
{
   t.numerator = 0;
   t.denominator= 0;
}

然后:

for(size_t i = 0; i < _dim; i++)
{
    inti(b[i]);
}