In this little code i wanna create a vector like std::vector, but implemented by myself. I'm sorry but i don't have idea about to solve it, if somebody can help me with that will be great.
template <typename T>
struct vector {
private:
typedef T type;
type *m_array;
size_t m_size;
public:
vector() { }
template <typename ... Args>
vector(Args&&... a) {
m_size = (size_t) sizeof...(a);
m_array = (type*) malloc ( m_size * sizeof(type) );
m_array = {a...};
}
~vector() {
free(m_array);
}
size_t size() {
return m_size;
}
type operator[](size_t pos) {
return m_array[pos];
}
};
template <typename ... Ts>
size_t count ( Ts ... args ) {
return (size_t)( sizeof...(args) );
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> v {3,2,4,5};
printf("The v size is: %d", v.size());
for(int i = 0; i < v.size(); ++i)
printf("Value for v [ %d ] : %d", i, v[i]);
system("pause>nul");
return 0;
}
The error in C++ Builder(Embarcadero) is the next:
[bcc32c Error] vector.cpp(29): excess elements in scalar initializer vector.cpp(49): in instantiation of function template specialization 'vector::vector' requested here