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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:34:19+00:00 2026-05-11T01:34:19+00:00

C++ does not have native support for lazy evaluation (as Haskell does). I’m wondering

  • 0

C++ does not have native support for lazy evaluation (as Haskell does).

I’m wondering if it is possible to implement lazy evaluation in C++ in a reasonable manner. If yes, how would you do it?

EDIT: I like Konrad Rudolph’s answer.

I’m wondering if it’s possible to implement it in a more generic fashion, for example by using a parametrized class lazy that essentially works for T the way matrix_add works for matrix.

Any operation on T would return lazy instead. The only problem is to store the arguments and operation code inside lazy itself. Can anyone see how to improve 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. 2026-05-11T01:34:20+00:00Added an answer on May 11, 2026 at 1:34 am

    I’m wondering if it is possible to implement lazy evaluation in C++ in a reasonable manner. If yes, how would you do it?

    Yes, this is possible and quite often done, e.g. for matrix calculations. The main mechanism to facilitate this is operator overloading. Consider the case of matrix addition. The signature of the function would usually look something like this:

    matrix operator +(matrix const& a, matrix const& b); 

    Now, to make this function lazy, it’s enough to return a proxy instead of the actual result:

    struct matrix_add;  matrix_add operator +(matrix const& a, matrix const& b) {     return matrix_add(a, b); } 

    Now all that needs to be done is to write this proxy:

    struct matrix_add {     matrix_add(matrix const& a, matrix const& b) : a(a), b(b) { }      operator matrix() const {         matrix result;         // Do the addition.         return result;     } private:     matrix const& a, b; }; 

    The magic lies in the method operator matrix() which is an implicit conversion operator from matrix_add to plain matrix. This way, you can chain multiple operations (by providing appropriate overloads of course). The evaluation takes place only when the final result is assigned to a matrix instance.

    EDIT I should have been more explicit. As it is, the code makes no sense because although evaluation happens lazily, it still happens in the same expression. In particular, another addition will evaluate this code unless the matrix_add structure is changed to allow chained addition. C++0x greatly facilitates this by allowing variadic templates (i.e. template lists of variable length).

    However, one very simple case where this code would actually have a real, direct benefit is the following:

    int value = (A + B)(2, 3); 

    Here, it is assumed that A and B are two-dimensional matrices and that dereferencing is done in Fortran notation, i.e. the above calculates one element out of a matrix sum. It’s of course wasteful to add the whole matrices. matrix_add to the rescue:

    struct matrix_add {     // … yadda, yadda, yadda …      int operator ()(unsigned int x, unsigned int y) {         // Calculate *just one* element:         return a(x, y) + b(x, y);     } }; 

    Other examples abound. I’ve just remembered that I have implemented something related not long ago. Basically, I had to implement a string class that should adhere to a fixed, pre-defined interface. However, my particular string class dealt with huge strings that weren’t actually stored in memory. Usually, the user would just access small substrings from the original string using a function infix. I overloaded this function for my string type to return a proxy that held a reference to my string, along with the desired start and end position. Only when this substring was actually used did it query a C API to retrieve this portion of the string.

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

Sidebar

Related Questions

Since silverlight does not have any support for DataSets/DataTables, what would be the best
It is my understanding that the java.regex package does not have support for named
VB 6.0 does not have any global handler.To catch runtime errors,we need to add
So Google Analytics does not have an API that we can use to get
Note that this function does not have a { and } body. Just a
I need a select from table which does not have column that tells when
I am working a project that does not have a trunk / branches /
Is there a method of accessing an Exchange server that does not have IMAP
I find it funny that Java (or the java.util library) does not have a
I'm looking for a profiler to use with native C++. It certainly does not

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.