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

  • Home
  • SEARCH
  • 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 6673565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:38:31+00:00 2026-05-26T03:38:31+00:00

code for (size_t i = 0; i < myClassObject.Method(); ++i) some work; I am

  • 0

code

for (size_t i = 0; i < myClassObject.Method(); ++i)
  some work;

I am wondering if the compiler will optimize out the method call and turn it into this?

size_t result = myClassObject.Method();
for (size_t i = 0; i < result; ++i
  some work;

Would it matter if it was a function call instead? My worry is that I could be doing 1000’s of unneeded calls to the method, but I prefer to write it with the method call in the condition.

  • 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-26T03:38:31+00:00Added an answer on May 26, 2026 at 3:38 am

    You should state in code what you want to do. If you need a sum, you wouldn’t write code that multiplies instead of adding just because you prefer to do so. It needs a smart compiler to realize that the call to Method() has no side effects, and that the loop of the body does not modify the parameters used within Method() directly or indirectly in any way.

    My personal preference, in order to do what I want and write it as I like it, is:

    for (size_t i = 0, result = myClassObject.Method(); i < result; ++i )
       some work;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some C++ code that prints a size_t : size_t a; printf(%lu, a);
I can't understand this result... The code: void foo(void * key, size_t key_sz) {
In some code I've inherited, I see frequent use of size_t with the std
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
What's wrong with this code? class School { public: template<typename T> size_t count() const;
Consider this code: LARGE_INTEGER l; size_t s; if (s < l.QuadPart) return 1; return
This is the relevant code: char c; size_t buffer_size = 0; wchar_t* wc =
I got some C code: typedef struct { size_t len; size_t alloclen; char *buf;
I have this code for curl: size_t write_to_string(void * ptr,size_t size,size_t nmeb,ResponseStruct * res_struct){
Is this valid/portable/legal code: class Vector3 { public: float& operator [] ( const size_t

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.