Initializer List in C++

Hi , how to create an initializer list for an array-like class ??
The class is using a template by the way ...
Something like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
#include <string>
#include <initializer_list>

template <typename T>
struct array
{
    array(std::initializer_list<T> in) : data(in), sum()
    {
        for(const auto& e: in)
            sum += e;
    }

    std::vector<T> data;
    T sum;
};

int main()
{
    array<double> foo {5.0, 3, 2.1};
    for(const auto& e: foo.data)
        std::cout << e << ' ';
    std::cout << '\n' << foo.sum << '\n';

    array<std::string> bar {"I ", "AM ", "GROOT"};
    for(const auto& e: bar.data)
        std::cout << e << ' ';
    std::cout << '\n' << bar.sum << '\n';
}

Will this work on C++ 98 projects?
No.
I want to write a C++ 98 project , please give me a C++ 98 code if you can.
I want to write a C++ 98 project

If you keep asking people for code, you won't exactly be writing it will you?
No, this is not possible in C++03. Create variable and then add needed values.
Topic archived. No new replies allowed.