Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:18:55+00:00 2026-05-23T02:18:55+00:00

I have read Effective C++ 3rd Edition written by Scott Meyers. Item 3 of

  • 0

I have read Effective C++ 3rd Edition written by Scott Meyers.

Item 3 of the book, “Use const whenever possible”, says if we want to prevent rvalues from being assigned to function’s return value accidentally, the return type should be const.

For example, the increment function for iterator:

const iterator iterator::operator++(int) {
    ...
}

Then, some accidents is prevented.

iterator it; 

// error in the following, same as primitive pointer
// I wanted to compare iterators
if (it++ = iterator()) {
  ...
}

However, iterators such as std::vector::iterator in GCC don’t return const values.

vector<int> v;
v.begin()++ = v.begin();   // pass compiler check

Are there some reasons for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:18:56+00:00Added an answer on May 23, 2026 at 2:18 am

    I’m pretty sure that this is because it would play havoc with rvalue references and any sort of decltype. Even though these features were not in C++03, they have been known to be coming.

    More importantly, I don’t believe that any Standard function returns const rvalues, it’s probably something that wasn’t considered until after the Standard was published. In addition, const rvalues are generally not considered to be the Right Thing To Do™. Not all uses of non-const member functions are invalid, and returning const rvalues is blanketly preventing them.

    For example,

    auto it = ++vec.begin();
    

    is perfectly valid, and indeed, valid semantics, if not exactly desirable. Consider my class that offers method chains.

    class ILikeMethodChains {
    public:
        int i;
        ILikeMethodChains& SetSomeInt(int param) {
            i = param;
            return *this;
        }
    };
    ILikeMethodChains func() { ... }
    ILikeMethodChains var = func().SetSomeInt(1);
    

    Should that be disallowed just because maybe, sometimes, we might call a function that doesn’t make sense? No, of course not. Or how about “swaptimization”?

    std::string func() { return "Hello World!"; }
    std::string s;
    func().swap(s);
    

    This would be illegal if func() produced a const expression – but it’s perfectly valid and indeed, assuming that std::string‘s implementation does not allocate any memory in the default constructor, both fast and legible/readable.

    What you should realize is that the C++03 rvalue/lvalue rules frankly just don’t make sense. They are, effectively, only part-baked, and the minimum required to disallow some blatant wrongs whilst allowing some possible rights. The C++0x rvalue rules are much saner and much more complete.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read Bloch's Item 69(Effective Java) and there was an example of Simple
I'm trying out Effective Java book and enjoying it. I read Builder pattern and
I want to use prepared statements. I have read that the advantage of prepared
I need help remembering what book I read this from, It might have been
I read Effective Java book and don't understand one paragraph where explained Clonable interface.
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have read various posts in stackoverflow and I am trying to figure out
I have read the Apple documentation, and many postings on the subject on SO,
I have read pretty much the entire documentation even beyond on the AWS AS
I have read few articles about table partioning but still I am bit confused

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.