I have a generic array class which throws a logic_error
if it's used with a non-primitive type.
Template Class:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
#define NULL_ELEMENT ((T)NULL)
template<class T> class Array
{
public:
Array(const int size)
{
this->elements[size];
this->size = size;
::fill_n(elements, size, NULL_ELEMENT); /* 1 */
}
// Output of the array
string toString()
{
int i=0;
stringstream ss;
ss << "Array{ ";
for( ; i<size-1; i++ )
{
ss << elements[i] << ", ";
}
ss << elements[i] << " }";
return ss.str();
}
// ...
private:
int size;
T elements[];
};
Test Code:
Working (primitive type used):
Array<int> arr(5);
cout << arr.toString() << endl;
Array is filled with 0
: Array{ 0, 0, 0, 0, 0 }
Fail (non-primitive type used):
Array<string> arr(size); // <-- Exception thrown here
cout << arr.toString() << endl;
Thrown Exception:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This happens in Array
class at when ::fill_()
is called (/* 1 */
).
I want to fill the whole array with the Null-Element of the type T
(like 0 if int or NULL
if pointer etc.) - without iterating over each element. memset()
is not a good solution here, isnt it?