what is the different between "static inline" and "static constexpr" in c++20 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

what is the different between "static inline" and "static constexpr" in c++20

How does INLINE and CONSTEXPR works and what is the difference between STATIC INLINE vs STATIC CONSTEXPR in c++20!?

17th Nov 2022, 8:55 AM
Dilip ishara
Dilip ishara - avatar
6 Answers
+ 3
inline: tells compiler to copy the code of this function and paste it inside the function that get called. It used for small functions mostly to avoid many call and jump instructions (at assembly level after compile) but can increase executable size. With some settings of the compiler may not do that everytime, so then can use __forceinline that guarantee will do that everytime. constexpr: will run at compile-time. In general constexpr can used when you want do something at compile-time. eg: constexpr int test = 23 * 2 + 10; In this case compiler will calculate the value of test when compiling and not at runtime. The same happend with functions. For example it used a lot for compile-time encryptions and hashing. Also if want run a function at compile-time, you need also a #define
17th Nov 2022, 9:48 AM
Giannis
Giannis - avatar
+ 2
#include <iostream> inline int var01 = 123; constexpr int var02 = 316; int main(int argc, char *argv[]) { std::cout << "<consoleStart>" << std::endl; return 0; } /* what is different about these variables!? not functions! Giannis ???
17th Nov 2022, 10:24 AM
Dilip ishara
Dilip ishara - avatar
+ 2
inline variable? is that even valid? inline only used for functions as i know
17th Nov 2022, 10:30 AM
Giannis
Giannis - avatar
+ 2
Inline variables are valid in C++! that is why i cannot i understand how i use them??? I read above answers of question, but i can't understand how i use it!!!
17th Nov 2022, 10:58 AM
Dilip ishara
Dilip ishara - avatar
+ 1
Do these variables behave same as functions?
17th Nov 2022, 10:27 AM
Dilip ishara
Dilip ishara - avatar