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
Post a Comment