I'm 16 years old and decided to start learning c++ a week ago. I would consider myself knowledgeable in other programming languages such as Python, C#, and Javascript. I've noticed that c++ has a lot of features that allows you to create intricate classes and programs. So I decided that I wanted to try to create a custom array class (data type/data structure), so that I could learn more about the language and its OOP. I have also been watching a lot of tutorials lately regarding c++, so I know the basics of the language.
The array that I'm trying to build is supposed to be allocated on the stack (not dynamic), it should take in any amount of elements of the same type, specified by the one using it. I don't want to assign the elements with the use of a for loop, I want to be able to specify the elements at array creation.
Array<int, 10> arr = { 32, 45, 12 };
Here I specify an instance of the Array class containing 10 elements of the integer type, where only 3 of the elements are assigned upon creation, leaving the other elements a value of 0xCCCCCCCC like they should be.
The issue comes when I make the constructor, it should be able to take in an arbitrary number of arguments up to the specified size of the array. I'm familiar with *args and **kwargs in Python, though it seems to be different in C++.
Here is the constructor:
template<typename... Args>
Array(Args&&... args)
{
/* Code to assign args to the array */
}
Upon debugging, I see that the elements that I entered show up like this:
| Name | Value | Type |
|------|-------|------|
| this | 0x0097fc00 {m_Data=0x0097fc00 {-858993460, -858993460, -858993460, -858993460, -858993460, -858993460, ...} } | Array<int,10> * |
| <args_0> | 32 | int && |
| <args_1> | 45 | int && |
| <args_2> | 12 | int && |
I have no idea as to how I would access the <args_x> variables.
So I googled a lot and found something called variadic templates. Though I don't understand it, trying to use std::forward didn't work in my faulty implementation.
I tried this:
template<typename... Args>
Array(Args&&... args)
{
args;
}
But I got an error message saying:
Error C3520 'args': parameter pack must be expanded in this context
I pressed the link (C3520) where I was taken to Microsoft, where it was stated:
Compiler error C3520 'identifier': parameter pack must be expanded in this context
Which was no use to me.
So I'm completely lost, and don't know what to do, which led me to post my problem here on Stack Overflow.
This is my first time submitting something to this website, so I am not sure if my description of the problem is up to expectations. Although I can give more information if needed.
Here is my full code:
#include <iostream>
#include <array>
using std::cout;
using std::cin;
using std::endl;
/*
* T = Type
* S = Size
*/
template<typename T, size_t S>
class Array {
public:
using value_type = T;
using size_type = size_t;
using different_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
constexpr int Size() const { return S; }
T& operator[](size_t index) { return m_Data[index]; }
const T& operator[](size_t index) const { return m_Data; }
template<typename... Args>
Array(Args&&... args)
{
args;
}
private:
T m_Data[S];
};
int main()
{
// My array
Array<int, 10> arr = { 32, 45, 12 };
// std::array used as reference
std::array<int, 10> test = { 32, 45, 12, 53 };
cin.get();
return 0;
}