compile time cast stack into vector

Hi. Everyone.

I want to ask, if it is possible, to cast a mount of memory allocated in the compile time into a std::vector?

1
2
3
4
5
  // I want to allocate such thing in the compile time.
  // maybe with static_cast
  std::vector index(1 << 16); // fix length vector

  // Solution? 
Hello CakeByTheOcean,

Yes.

I believe what you want is: std::vector index(16);. This will create a vector with (16) elements that you could then use a subscript to access.

You could also use a constant variable or a regular variable in place of the (16).

Hope that helps,

Andy
compile time fix length vector ist std::array
A std::vector's size can be set at compile time, but once created the number of elements can be altered.

http://www.cplusplus.com/reference/vector/vector/

Well, not entirely true, a const std::vector can't be altered in size. Neither can the contents. Making a const std::vector of little use.

a std::array's size is set at compile time and is unalterable.

http://www.cplusplus.com/reference/array/array/

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <array>

int main()
{
   std::cout << "std::vector:\n";

   const int arr_size { 5 };

   // a compile-time sized std::vector is default zero-filled
   std::vector<int> vec1(arr_size);

   for (auto& itr : vec1) { std::cout << itr << ' '; }
   std::cout << '\n';

   // a vector's size and contents can be altered at run-time
   vec1[2] = 44;
   vec1.push_back(8);
   vec1.insert(vec1.begin(), 22);

   for (auto& itr : vec1) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   // creating a compile time sized std::vector from an initializer list
   std::vector<int> vec2 { 1, 2, 3, 4, 5 };

   for (auto& itr : vec2) { std::cout << itr << ' '; }
   std::cout << '\n';

   vec2[2] = 44;
   vec2.push_back(8);
   vec2.insert(vec2.begin(), 22);

   for (auto& itr : vec2) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   // const std::vector<int> vec3(arr_size) { 1, 2, 3, 4, 5 }; // error!
   const std::vector<int> vec3 { 1, 2, 3, 4, 5 };

   for (auto& itr : vec3) { std::cout << itr << ' '; }
   std::cout << '\n';

   // can't alter the size or contents of a const std::vector
   // vec3[2] = 44;
   // vec3.push_back(8);
   // vec3.insert(vec3.begin(), 22);

   for (auto& itr : vec3) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   std::cout << "std::array:\n";

   std::array<int, arr_size> arr1;

   for (auto& itr : arr1) { std::cout << itr << ' '; }
   std::cout << '\n';

   arr1[2] = 44;

   for (auto& itr : arr1) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   std::array<int, arr_size> arr2 { 1, 2, 3, 4, 5 };

   for (auto& itr : arr2) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   arr2[2] = 44;

   for (auto& itr : arr2) { std::cout << itr << ' '; }
   std::cout << '\n';
}

(Code::Blocks output:)
std::vector:
0 0 0 0 0
22 0 0 44 0 0 8

1 2 3 4 5
22 1 2 44 4 5 8

1 2 3 4 5
1 2 3 4 5

std::array:
7143296 7143372 1958595776 -2085710404 -2
7143296 7143372 44 -2085710404 -2

1 2 3 4 5

1 2 44 4 5

Visual Studio 2019 (and VS 2017) does some weird things with memory allocation, apparently with const std::vectors and any memory allocated following:
std::vector:
0 0 0 0 0
22 0 0 44 0 0 8

1 2 3 4 5
22 1 2 44 4 5 8

1 2 3 4 5
1 2 3 4 5

std::array:
1 2 3 4 5
1 2 44 4 5

1 2 3 4 5

1 2 44 4 5
Last edited on
Topic archived. No new replies allowed.