struct bar {
const int* x;
};
bar make_bar(const int& x){
return {&x};
}
std::map<int,bar> data;
shuffle(data);
knowing that bar
will never modify x
(or cause it to be modified) in its lifetime requires understanding every use of bar
in the program, or, say, making x
a pointer to const
.
Even with perfect whole program optimization (which cannot exist: turing machines are not perfectly understandable), dynamic linking means you cannot know at compile time how data will be used. const
is a promise, and breaking that promise (in certain contexts) can be UB. The compiler can use that UB to optimize in ways that ignores the promise being broken.
inline
is not obsolete: it means the same thing it ever did, that linker collisions of this symbol are to be ignored, and it mildly suggests injecting the code into the calling scope.
const
simplifies certain optimizations (which may make them possible), and enforces things on the programmer (which helps the programmer), and can change what code means (const
overloading).
const
is to prevent future you to mistakenly modify a value. When reading a long function, if you seeconst int x
at the start, you know that at line 300x
is still the same even without checking the previous code. – Rangedconst
for the compiler so much as you type it for yourself. You are telling the compiler to kick you if you ever try to change its value. – Uroscopya
andb
are not assigned to and non-volatile
, thus that they are effectivelyconst
, and act accordingly.const
is far more powerful when it is applied to pointers, or is part of a function's interface (its signature), since then a compiler can rely on the promises of the function to optimize. – Unmentionableinline
is far from obsolete. It allows for multiple definitions of the function to exist in the program without violating ODR. – Harryharshinline
, it is a request that the compiler is allowed to ignore: the compiler might inline-expand some, all, or none of the places where you call a function designated asinline
." Do you disagree with that? – Hortainline
keyword, which does more than just this ignorable request for inlining. – Harryharshinline
being used in reference to functions/methods. – Hortaconst
. – Hortaconst
exists. – Hodgesinline
not being a ignored? Doesn't the compiler do the exact same thing withinline
d and non-inline
d functions? – Hortainline
, and include that header in more than one source file, you'll get a linker error. You will not get that error if you do mark it asinline
. – Harryharshint f() { return 42; } int main() { return f(); }
. File2:int f() { return 42; }
. Both linked into the executable. – Harryharshinline
solve that? I'm definitely seeing an error there whether or not you useinline
. – Hortaf
s asinline
, it must link correctly. That is the semantics of theinline
keyword, and "inline functions" in C++. It allows the function to be defined in more than one translation unit (such as when you include a header with its definition). – Harryharshinline
a necessary keyword. I've opened a question here: https://mcmap.net/q/234810/-is-there-still-a-use-for-inline-duplicate/2642059 I want to understand what you're saying, I was hoping that given the ability to answer rather than comment you could convert me to aninline
disciple. – Horta