Thread: const return types, or lack thereof

  1. #1
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20

    const return types, or lack thereof

    From what I gather, functions return temporaries which don't need to be const (I assume making it const would use more processing) except for cases where returns are pointers or references.

    I have no problem understanding why pointers would warrant such sensitivity. It's a common algorithm to move a pointer instead of calculating the new position from the start every-time. So just as easily as they can moved appropriately, they can be moved inappropriately.

    Where I don't understand is the supposed need to make references const, I thought references were basically pointers that couldn't change once initiated and were required to be initiated. If something can't be change inherently, where's the need to repeat the requirement (and add processing) ?

    Thanks

    Sorry if I'm duplicating an issue, I couldn't find one alike with the search results I got from
    Type: Posts; Keyword(s): return const; Forum = C++ Programming and child forums
    Type: Posts; Keyword(s): return+const; Forum = C++ Programming and child forums
    Type: Posts; Keyword(s): return AND const; Forum = C++ Programming and child forums
    Note that choosing a differently using the Keyword drop-down (Search Entire Posts / Search Titles Only) display the same exact search characteristics (what I'm calling what's quoted).

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have no problem understanding why pointers would warrant such sensitivity. It's a common algorithm to move a pointer instead of calculating the new position from the start every-time. So just as easily as they can moved appropriately, they can be moved inappropriately.
    And sometimes pointers and references refer to objects that shouldn't be changed at all, like literal strings.
    Code:
    const char *months[] = {"jan", "feb", "mar" /* ... */ };
    Remove the const, create trouble....

    The literal string is just one example. You will probably make your own constants in your programs, and create aliases for those constants.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DynV
    I have no problem understanding why pointers would warrant such sensitivity. It's a common algorithm to move a pointer instead of calculating the new position from the start every-time. So just as easily as they can moved appropriately, they can be moved inappropriately.
    My guess is that most of the time when you see a const qualified pointer type as the return type of a function, the type is pointer to const rather than pointer that is const ("const pointer").

    Quote Originally Posted by DynV
    Where I don't understand is the supposed need to make references const, I thought references were basically pointers that couldn't change once initiated and were required to be initiated. If something can't be change inherently, where's the need to repeat the requirement (and add processing) ?
    Ah, but "const reference" actually means "reference to const", not "reference that is const".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by laserlight View Post
    My guess is that most of the time when you see a const qualified pointer type as the return type of a function, the type is pointer to const rather than pointer that is const ("const pointer").
    You're right and I usually detect it but that darn const-on-the-left-at-beggining-of-statements exception confused me! >
    Code:
    const T& operator*() const;
    I always put const on the right.

    Quote Originally Posted by whiteflags View Post
    And sometimes pointers and references refer to objects that shouldn't be changed at all, like literal strings.
    I'm not sure what you mean but I assume my previous reply (in the same post) could be of use.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by DynV View Post
    I assume making it const would use more processing
    const is an entirely compile-time concept. if anything, it would require greater time to compile, but I seriously doubt it. the runtime performance would be identical, and possibly better in some cases.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well I always thought I was one of the clearer folks on here. I can help you understand pointers and constness but I'm not sure if I can clarify my statement any further.

    Pointers offer two levels of constness. One level, the lesser used in my opinion, relates to the pointer value.
    >> char *const pch = right;
    The const on the right of the star will prevent the pointer from moving, to use your words.
    >> const char *pch = right;
    This prevents code from changing the object that is being pointed to (called right here).

    The levels of const mean different things, and if you combine both, you get a pointer that can only point to one thing, and dereference that one thing in a read only context.

    References are much simpler. It is true that they don't move, but you can still use a reference to change an underlying object. Therefore, we can say that a reference may need to be declared const for purposes of const correctness. "const" is a qualifier keyword. Anything without it, can't be considered constant.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    so to extend this:

    Code:
    const char *const pch = right;
    would be a const pointer to const data, correct?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elkvis
    would be a const pointer to const data, correct?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Combining Functions with different return types
    By Phyxashun in forum C++ Programming
    Replies: 5
    Last Post: 12-29-2008, 01:47 AM
  2. Why return a const value from a function?
    By 6tr6tr in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2008, 08:55 AM
  3. inline efficiency, or lack thereof
    By Smallz in forum C++ Programming
    Replies: 14
    Last Post: 09-02-2006, 08:25 PM
  4. Two Return Types???
    By DaRetard in forum Windows Programming
    Replies: 10
    Last Post: 06-07-2004, 07:11 AM
  5. casting c++,const in class types,function return values...
    By ABitLazy in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2004, 03:44 PM

Tags for this Thread