+ 2

What's the difference between const and constexpr?

where they use? how they use?

31st Jul 2017, 2:24 PM
Emi
Emi - avatar
4 Respostas
+ 10
'const' declares that an identifier used as a variable is a constant, the value held by that variable cannot be changed. 'constexpr' declares that the value of a variable/function can be determined during compile time, and said variable/function can appear within constant expressions. https://stackoverflow.com/questions/4748083/when-should-you-use-constexpr-capability-in-c11 http://en.cppreference.com/w/cpp/language/constexpr
31st Jul 2017, 5:50 PM
Hatsy Rei
Hatsy Rei - avatar
+ 10
You *can* replace const with constexpr, but note that they are not identical. This is similar to a case of using long int over int - You can store values with long int, including and beyond what int is capable of storing, but we don't replace one with another because it doesn't necessarily reflect what we want to do. https://stackoverflow.com/questions/15078689/should-i-always-replace-const-int-with-constexpr-int-in-c11-whenever-possi
1st Aug 2017, 4:04 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
@Hatsy Rei is that mean constexpr have include all the feature that const have, but const didn't. means we can replace const with constexpr?
31st Jul 2017, 11:04 PM
Emi
Emi - avatar
+ 1
@Hatsy Rei tq for your informative and detailed answer 😊😊
1st Aug 2017, 9:27 AM
Emi
Emi - avatar