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 8205887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:26:49+00:00 2026-06-07T08:26:49+00:00

Variant a: const auto end = whatever.end(); for (auto it = whatever.begin(); it !=

  • 0

Variant a:

const auto end = whatever.end();
for (auto it = whatever.begin(); it != end; ++it)
{
    // ...
}

Variant b:

const auto end = whatever.cend(); // note the call to cend insteand of end here
for (auto it = whatever.begin(); it != end; ++it)
{
    // ...
}

Is there any reason to believe that variant b will be less efficient than variant a, since the loop condition compares two different kinds of iterators? Does this cause an implicit conversion on it?

(end is used multiple times inside the for loop, hence my desire to hoist it out.)

  • 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-06-07T08:26:50+00:00Added an answer on June 7, 2026 at 8:26 am

    In principle, it could be less efficient and result in an implicit conversion with non-zero cost.

    In practice, iterator and const_iterator are likely to participate in an inheritance relationship (either one derives from the other, or both derive from an _iterator_base) such that the inequality operator is defined on the base class and there is no need for an implicit conversion (instead, the more-derived iterator gets ignored). Even in the absence of these, the conversion is likely to be trivial enough to be inlined and optimised out.

    libstdc++ optimises these comparisons differently, by defining operator== and operator!= between iterator and const_iterator: http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a02037.html#l00295

    libc++ doesn’t have any optimisation: http://llvm.org/svn/llvm-project/libcxx/trunk/include/__tree – although again the constructor of const_iterator from iterator is so trivial that I’d expect it to be completely optimised out.

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

Sidebar

Related Questions

struct hostent *gethostbyname(const char *name) Note that hostent.h_addr_list is a field with variant length.
There are several variant questions on this on SO, but I did not find
If all the types in my boost::variant support the same method, is there a
VBA (and I assume VB) has a Variant type which I believe takes up
Is there an exec variant that will use the current application directory to locate
Is there a standard way to convert between TVarRec and Variant values? I want
I'm creating a variant of a chess-like program that needs to simultaneously generate and
I have a variant for initializing DbRecordData public class DbRecordData : IList<DbFieldValue>, ICollection<DbFieldValue>, IEnumerable<DbFieldValue>,
If I have a variant array which holds nothing but simple types, and possible
Given two identical boost::variant instances a and b , the expression ( a ==

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.