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

The Archive Base Latest Questions

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

Here’s one thing I haven’t seen explicitly addressed in C++ expression template programming in

  • 0

Here’s one thing I haven’t seen explicitly addressed in C++ expression template programming in order to avoid building unnecessary temporaries (through creating trees of “inlinable templated objects” that only get collapsed at the assignment operator). Suppose for the illustration we’re modeling 1-D sequences of values, with elementwise application of arithmetic operators like +, *, etc. Call the basic class for fully-created sequences Seq (which holds a fixed-length list of doubles for the sake of concreteness) and consider the following illustrative pseudo-C++-code.

void f(Seq &a,Seq &b,Seq &c,Seq &d,Seq &e){
    AType t=(a+2*b)/(a+b+c); // question is about what AType can be
    Seq f=d*t;
    Seq g=e*e*t;
    //do something with f and g
}

where there are expression templated overloads for +, etc, elsewhere. For the line defining t:

  • I can implement this code if I make AType be Seq, but then I’ve created this full intermediate variable when I don’t need it (except in how it enables computation of f and g). But at least it’s only calculated once.

  • I can also implement this making AType be the appropriate templated expression type, so that a full Seq isn’t created at the commented line, but consumed chunk-by-chunk in f and g. But then the same computation involved in creating every particular chunk will be repeated in both f and g. (I suppose in theory an incredibly smart compiler might realise the same computation is being done twice and CSE-it, but I don’t think any do and I wouldn’t want to rely on an optimiser always being able to spot the opportunities.)

My understanding is that there’s no clever code rewriting and/or usage of templates that allow each chunk of t to be calculated only once and for t to be calculated chunkwise rather than all at once?

(I can vaguely imagine AType could be some kind of object that contains both an expression template type and a cached value that gets written after it’s evaluated the first time, but that doesn’t seem to help with the need to synchronise the two implicit loops in the assignments to f and g.)

In googling, I have come across one Masters thesis on another subject that mentions in passing that manual “common subexpression elimination” should be avoided with expression templates, but I’d like to find a more authoritative “it’s not possible” or a “here’s how to do it”.

The closest stackoverflow question is Intermediate results using expression templates
which seems to be about the type-naming issue rather than the efficiency issue in creating a full intermediate.

  • 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-25T02:23:10+00:00Added an answer on May 25, 2026 at 2:23 am

    Since you obviously don’t want to do the entire calculation twice, you have to cache it somehow. The easiest way to cache it seems to be for AType to be a Seq. You say This has the downside of a full intermediate variable, but that’s exactly what you want in this case. That full intermediate is your cache, and cannot be trivially avoided.

    If you profile the code and this is a chokepoint, then the only faster way I can think of is to write a special function to calculate f and g in parallell, but that’d be super-confusing, and very much not recommended.

    void g(Seq &d, Seq &e, Expr &t, Seq &f, Seq &g) 
    {
        for(int i=0; i<d.size(); ++i) {
            auto ti = t[i];
            f[i] = d[i]*ti;
            g[i] = e[i]*e[i]*ti;
        }
    }
    void f(Seq &a,Seq &b,Seq &c,Seq &d,Seq &e) 
    {
        Expr t = (a+2*b)/(a+b+c);
        Seq f, g;
        g(d, e, t, f, g);
        //do something with f and g
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a coding problem for those that like this kind of thing. Let's see
Here's my scenario. I've got SharePoint 2010 running on one server, with an SQL
Here's a dump of the stats provided my mod_pagespeed from one of my sites.
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote

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.