codekiddy
codekiddy

Reputation: 6137

initialize const vector with const initializer list

#include <vector>

int main()
{
    typedef const std::vector<const int> set_t;
    set_t Low = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};

    return 0;
}

When compiling the above code I got trillion of errors from STL headers.

What I want to do here is to initialize a vector and ensure that values can not be changed at some point later and also to make sure that no new values can be added.

This vector should be created once with initial values and not changed in any way.

What's wrong here?

Upvotes: 3

Views: 487

Answers (1)

Drew Dormann
Drew Dormann

Reputation: 63839

This is also a const vector, and it will let your code compile.

typedef const std::vector<int> set_t;

Making the std::vector alone const will expose only the non-modifying interface. Your ints will not change.

Upvotes: 3

Related Questions