const-correctness

C++ Pass By Const Reference and Return By Const Reference

别等时光非礼了梦想. 提交于 2019-12-01 03:06:22
I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this: unsigned long factorial(unsigned long n) { return (n == 0) ? 1 : n * factorial(n - 1); } I'm assuming that there will be a performance increase when we pass by const reference and we return a const reference... but const -correctness always confuses me. const unsigned long & factorial(const unsigned long& n) { return (n == 0) ? 1 : n * factorial(n - 1); } Is it valid to return a const reference? Furthermore, could somebody please tell me: is it beneficial?

Trailing return types, decltype and const-ness

坚强是说给别人听的谎言 提交于 2019-12-01 02:28:14
I was merily experimenting with the new trailing return types, where I hit a problem with this (simplified) code #include <list> class MyContainer{ std::list<int> ints; auto begin( ) -> decltype(ints.begin()) { return ints.begin(); } auto begin( ) const -> decltype(ints.begin()) { return ints.begin(); } }; Ignore the fact of how pointless this code is. The important part is the compiler error generated when using GCC 4.6.1 (with -std=c++0x flag): In member function 'std::list<int>::iterator MyContainer::begin() const': error: could not convert '((const MyContainer*)this)->MyContainer::ints.std

implementing a variadic zip function with const-correctness

限于喜欢 提交于 2019-11-30 22:35:43
I'm trying to implement a zip function. zip 's parameters are each wrapped<Ti> , where Ti varies from parameter to parameter. zip takes these wrapped<Ti> s and produces a wrapped<tuple<T1&,T2&,...TN&>> , or in other words a wrapped tuple of references to its parameters. The references should preserve const -ness. Here's my first stab at zip with one parameter, which doesn't work in general: #include <utility> #include <tuple> // implement forward_as_tuple as it is missing on my system namespace ns { template<typename... Types> std::tuple<Types&&...> forward_as_tuple(Types&&... t) { return std:

Why is the endptr parameter to strtof and strtod a pointer to a non-const char pointer?

爱⌒轻易说出口 提交于 2019-11-30 17:06:32
The standard C library functions strtof and strtod have the following signatures: float strtof(const char *str, char **endptr); double strtod(const char *str, char **endptr); They each decompose the input string, str , into three parts: An initial, possibly-empty, sequence of whitespace A "subject sequence" of characters that represent a floating-point value A "trailing sequence" of characters that are unrecognized (and which do not affect the conversion). If endptr is not NULL , then *endptr is set to a pointer to the character immediately following the last character that was part of the

Why is the endptr parameter to strtof and strtod a pointer to a non-const char pointer?

断了今生、忘了曾经 提交于 2019-11-30 16:30:42
问题 The standard C library functions strtof and strtod have the following signatures: float strtof(const char *str, char **endptr); double strtod(const char *str, char **endptr); They each decompose the input string, str , into three parts: An initial, possibly-empty, sequence of whitespace A "subject sequence" of characters that represent a floating-point value A "trailing sequence" of characters that are unrecognized (and which do not affect the conversion). If endptr is not NULL , then *endptr

How to call a non-const method from a const method?

浪尽此生 提交于 2019-11-30 11:39:43
I've got a const method in my class, which cannot be changed to non-const. In this method, I need to call a non-const method but the compiler doesn't let me do that. Is there any way around it? Here is a simplified sample of my code: int SomeClass::someMethod() const { QColor saveColor = color(); setColor(QColor(255,255,255)); // Calling non-const method // .... setColor(saveColor); // restore color return 1; } One of the challenges of doing const -correctness is you can't do it halfway. It's either all or nothing. If you try to do it halfway, you end up in a tough spot like you are here. You

Why is const-correctness specific to C++?

[亡魂溺海] 提交于 2019-11-30 10:34:09
问题 Disclaimer: I am aware that there are two questions about the usefulness of const-correctness, however, none discussed how const-correctness is necessary in C++ as opposed to other programming languages. Also, I am not satisfied with the answers provided to these questions. I've used a few programming languages now, and one thing that bugs me in C++ is the notion of const-correctness. There is no such notion in Java, C#, Python, Ruby, Visual Basic, etc., this seems to be very specific to C++.

Invoking a nonconst method on a member from a const method

最后都变了- 提交于 2019-11-30 08:37:48
问题 I was surprised to find this "hole" in "const"ness: #include <stdio.h> class A { int r ; public: A():r(0){} void nonconst() { puts( "I am in ur nonconst method" ) ; r++; } } ; class B { A a ; A* aPtr ; public: B(){ aPtr = new A() ; } void go() const { //a.nonconst() ; // illegal aPtr->nonconst() ; //legal } } ; int main() { B b ; b.go() ; } So basically from const method B::go() , you can invoke the non-const member function (aptly named nonconst() ) if object of type A is referenced by a

What is meaning of a pointer to a constant function?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 08:02:39
Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data. Pointers can be defined to point to a function. My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const with function pointers. Here are some questions: What is the meaning of a pointer to a constant function versus a pointer to a non-constant function? Can a function be const? Can a function be non-const (mutable)? What is the proper (safe) syntax for passing a function pointer? Edit 1: Function pointer syntax typedef void (*Function

Does it ever make sense to make a fundamental (non-pointer) parameter const?

末鹿安然 提交于 2019-11-30 06:51:51
I recently had an exchange with another C++ developer about the following use of const : void Foo(const int bar); He felt that using const in this way was good practice. I argued that it does nothing for the caller of the function (since a copy of the argument was going to be passed, there is no additional guarantee of safety with regard to overwrite). In addition, doing this prevents the implementer of Foo from modifying their private copy of the argument. So, it both mandates and advertises an implementation detail. Not the end of the world, but certainly not something to be recommended as