const-correctness

Does const-correctness give the compiler more room for optimization?

你说的曾经没有我的故事 提交于 2019-11-26 14:27:47
I know that it improves readability and makes the program less error-prone, but how much does it improve the performance? And on a side note, what's the major difference between a reference and a const pointer? I would assume they're stored in the memory differently, but how so? Nemo [Edit: OK so this question is more subtle than I thought at first.] Declaring a pointer-to-const or reference-of-const never helps any compiler to optimize anything. (Although see the Update at the bottom of this answer.) The const declaration only indicates how an identifier will be used within the scope of its

Const correctness for array pointers?

旧时模样 提交于 2019-11-26 14:21:52
问题 Someone made an argument saying that in modern C, we should always pass arrays to functions through an array pointer, since array pointers have strong typing. Example: void func (size_t n, int (*arr)[n]); ... int array [3]; func(3, &array); This sounded like it could potentially be a good idea to prevent all kinds of type-related and array-out-of-bounds bugs. But then it occurred to me I don't know how to apply const correctness to this. If I do void func (size_t n, const int (*arr)[n]) then

Why can't I convert 'char**' to a 'const char* const*' in C?

无人久伴 提交于 2019-11-26 11:46:42
The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc & g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care): char **a; const char** b = a; I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows reassignment of the pointers and prevents you from circumventing const-correctness ( C++ FAQ ). char **a; const char* const* b = a; However, in pure C, the corrected version (using const char * const *) still gives a warning, and I don't understand why. Is there a

What is the reason behind cbegin/cend?

拈花ヽ惹草 提交于 2019-11-26 10:09:19
问题 I wonder why cbegin and cend were introduced in C++11? What are cases when calling these methods makes a difference from const overloads of begin and end ? 回答1: It's quite simple. Say I have a vector: std::vector<int> vec; I fill it with some data. Then I want to get some iterators to it. Maybe pass them around. Maybe to std::for_each : std::for_each(vec.begin(), vec.end(), SomeFunctor()); In C++03, SomeFunctor was free to be able to modify the parameter it gets. Sure, SomeFunctor could take

Const correctness for value parameters

北慕城南 提交于 2019-11-26 09:09:59
问题 I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the function. This is fine: // header int func(int i); // cpp int func(const int i) { return i; } Is doing this really a best practice? Because I\'ve never seen anyone do it. I\'ve seen this quotation (not sure of the source) in other places this has been

What are the use cases for having a function return by const value for non-builtin type?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 07:29:15
问题 Recently I have read that it makes sense when returning by value from a function to qualify the return type const for non-builtin types, e.g.: const Result operation() { //..do something.. return Result(..); } I am struggling to understand the benefits of this, once the object has been returned surely it\'s the callers choice to decide if the returned object should be const? 回答1: Basically, there's a slight language problem here. std::string func() { return "hai"; } func().push_back('c'); //

Does const-correctness give the compiler more room for optimization?

血红的双手。 提交于 2019-11-26 05:56:03
问题 I know that it improves readability and makes the program less error-prone, but how much does it improve the performance? And on a side note, what\'s the major difference between a reference and a const pointer? I would assume they\'re stored in the memory differently, but how so? 回答1: [Edit: OK so this question is more subtle than I thought at first.] Declaring a pointer-to-const or reference-of-const never helps any compiler to optimize anything. (Although see the Update at the bottom of

Modifying a const through a non-const pointer

我的未来我决定 提交于 2019-11-26 04:28:30
问题 I\'m a bit confused what happened in the following code: const int e = 2; int* w = ( int* ) &e; // (1) cast to remove const-ness *w = 5; // (2) cout &lt&lt *w &lt&lt endl; // (3) outputs 5 cout &lt&lt e &lt&lt endl; // (4) outputs 2 cout &lt&lt \"w = \" &lt&lt w &lt&lt endl; // (5) w points to the address of e cout &lt&lt \"&e = \" &lt&lt &e &lt&lt endl; In (1), w points to the address of e. In (2), that value was changed to 5. However, when the values of *w and e were displayed, their values

Why can&#39;t I convert &#39;char**&#39; to a &#39;const char* const*&#39; in C?

匆匆过客 提交于 2019-11-26 02:36:13
问题 The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc & g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care): char **a; const char** b = a; I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows reassignment of the pointers and prevents you from circumventing const-correctness (C++ FAQ). char **a; const char* const* b = a; However, in pure C, the

Why isn&#39;t it legal to convert “pointer to pointer to non-const” to a “pointer to pointer to const”

随声附和 提交于 2019-11-26 01:26:51
问题 It is legal to convert a pointer-to-non-const to a pointer-to-const. Then why isn\'t it legal to convert a pointer to pointer to non-const to a pointer to pointer to const ? E.g., why is the following code illegal: char *s1 = 0; const char *s2 = s1; // OK... char *a[MAX]; // aka char ** const char **ps = a; // error! 回答1: From the standard: const char c = 'c'; char* pc; const char** pcc = &pc; // not allowed *pcc = &c; *pc = 'C'; // would allow to modify a const object 回答2: Ignoring your code