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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:18:23+00:00 2026-06-04T17:18:23+00:00

(For a concrete compiler/platform context take GCC 4.7 and Ubuntu 12.04 on x86_64) Given

  • 0

(For a concrete compiler/platform context take GCC 4.7 and Ubuntu 12.04 on x86_64)

Given some function f:

void f(int x, int y);

int nx = ...;
int ny = ...;

One way to iterate over every value (x,y) from (0,0) to (nx,ny) is:

for (int x = 0; x < nx; x++)
    for (int y = 0; y < ny; y++)
        f(x,y);

Let this compile to some generated code Q1.

We will write a function g such that:

for (auto it : g(Z))
    f(it.x, it.y);

compiles to code Q2.

Is it possible to write g such that Q2 is as efficient as Q1? If yes, how? If not, what is the closest we can get?

You may change auto to auto& or auto&& if it helps.

You may also change it.x to it.x(), and it.y to it.y(), if it helps.

(Recall that the expansion of range-based for is just an iterator-like type of your choosing: C++11: The range-based for statement: "range-init" lifetime?)

  • 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-04T17:18:24+00:00Added an answer on June 4, 2026 at 5:18 pm

    Is it possible to write g such that Q2 is as efficient as Q1? If yes, how? If not, what is the closest we can get?

    Sure its possible, you just need to define iterators that increment in the same way as your for loop. From the top of my head:

    class matrix_iterator
    {
    public:
        ...
    
        matrix_iterator& operator++()
        {
            if( ++y >= ny )
            {
                ++x;
                y = 0;
            }
    
            return *this;
        }
    
    private:
        int nx, ny;
        int x, y;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have 2 concrete methods in 2 abstract classes. One class contains the
I have extracted some of my concrete classes into interfaces I used to have
I am writing an abstract matrix class (and some concrete subclasses) for use on
An abstract function must be implemented by all concrete classes. Sometimes you want to
Section 6.3 of the TypeScript language spec talks about function overloading and gives concrete
Given the following, I would not expect the compiler to allow multiple attributes that
I have a situation where I'd like the behaviour of the compiler explained. Given
I'm using an MPC56XX (embedded systems) with a compiler for which an int and
Given this code public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFolder +
I'm hoping there's some clever way to do this. I have a generic base

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.