c++ - why constant variables are not treated as compile time constant sometime -


this question has answer here:

i tried execute 2 different scenarios :

scenario 1:

const auto arraysize = 10; // fine, arraysize constant  std::array<int, arraysize> data; 

here , arraysize treated compile time constant , hence allowed in std::array .

scenario 2:

int sz=10; const auto arraysize = sz; // fine . std::array<int, arraysize> data; //error , arraysize not compile time constant . 

in scenario 2 , arraysize not treated compile time constant despite of fact arrysize constant copy of sz .

why these 2 scenarios treated differently ?

because can like

int sz = 0; std::cin >> sz; const auto arraysize = sz; 

and here value of sz defined in runtime. can use constexpr, instead of const, compile error on such initialization.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -