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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:07:14+00:00 2026-06-08T15:07:14+00:00

Consider a standard use of the CRTP , for some expression template mechanism, which

  • 0

Consider a standard use of the CRTP, for some expression template mechanism, which keeps its children by value:

template <typename T, typename>
struct Expr {};

template <typename T>
struct Cst : Expr<T, Cst<T>> 
{
    Cst(T value) : value(std::move(value)) {}

private:
    T value;
};

template <typename T, typename L, typename R>
struct Add : Expr<T, Add<T, L, R>> 
{
    Add(L l, R r) : l(std::move(l)), r(std::move(r))

private:
    L l; R r;
};

etc.

Now, when implementing the operator, we must pass by reference, since the argument is to be downcast to the right type. The problem is that I find myself implementing four (!) versions of operator+:

template <typename T, typename L, typename R>
Add<T, L, R> operator+(Expr<T, L>&& l, Expr<T, R>&& r)
{
    return Add<T, L, R>(
        std::move(static_cast<L&>(l)),
        std::move(static_cast<R&>(r)));
}

template <typename T, typename L, typename R>
Add<T, L, R> operator+(const Expr<T, L>& l, Expr<T, R>&& r)
{
    return Add<T, L, R>(
        static_cast<const L&>(l),
        std::move(static_cast<R&>(r)));
}

template <typename T, typename L, typename R>
Add<T, L, R> operator+(Expr<T, L>&& l, const Expr<T, R>& r)
{
    return Add<T, L, R>(
        std::move(static_cast<L&>(l)),
        static_cast<const R&>(r));
}

template <typename T, typename L, typename R>
Add<T, L, R> operator+(const Expr<T, L>& l, const Expr<T, R>& r)
{
    return Add<T, L, R>(
        static_cast<const L&>(l),
        static_cast<const R&>(r));
}

Indeed, if the goal is to minimize needless copying, one has to distinguish between temporaries (that can be moved) and lvalues (which must be copied), hence the four overloads.

In C++03, there is “no problem”: we use const references and copy all the time, period. In C++11, we can do better, and it is the goal here.

Is there some trick that would allow me to write the addition logic once, or is writing a macro my best option here (since the logic will be repeated for other operators) ?

I’m also open to other suggestions about how to write expression templates with C++11. Just consider that the goal is to minimize copying, since the values stored in terminal nodes may be huge numbers, or matrices (in my precise case, terminal nodes may contain several megabytes of interpolated data, and copy is disabled for these objects — for other objects, copying is possible).

  • 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-08T15:07:16+00:00Added an answer on June 8, 2026 at 3:07 pm

    Here is another approach to writing expression templates that allows the arguments to be passed by value:

    template <typename T>
    struct Expr : T {
      Expr(T value) : T(value) { }
    };
    
    template <typename A,typename B>
    struct Add {
      A a;
      B b;
    
      Add(A a,B b) : a(a), b(b) { }
    };
    
    template <typename A,typename B>
    Expr<Add<A,B> > operator+(Expr<A> a,Expr<B> b)
    {
      return Expr<Add<A,B> >(Add<A,B>(a,b));
    }
    

    There are a lot of implied copies, but I’ve found that the compiler does an excellent job of removing them.

    To also make it convenient to use constants, you can write additional overloads:

    template <typename A,typename B>
    Expr<Add<Constant<A>,B> > operator+(const A& a,Expr<B> b)
    {
      return Expr<Add<Constant<A>,B> >(Add<Constant<A>,B>(a,b));
    }
    
    template <typename A,typename B>
    Expr<Add<A,Constant<B> > > operator+(Expr<A> a,const B& b)
    {
      return Expr<Add<A,Constant<B> > >(Add<A,Constant<B> >(a,b));
    }
    

    where Constant is a class template, such as:

    template <typename T>
    struct Constant {
      const T& value;
      Constant(const T& value) : value(value) { }
    };
    

    There are a lot of implied copies, but I’ve found that the compiler does an excellent job of removing them.

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

Sidebar

Related Questions

Consider the following 3 standard statements $queryString = SOME SQL SELECT QUERY; $queryResult =
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
I am going through Effective Java and some of my things which I consider
Two use cases for which I would consider short come to mind: I want
Consider this is a standard Linq from q in SomeQuery select new SomeObject {
Are there some standards that you consider to be so obvious that they would
Consider a standard iterator where it is necessary to allocate memory for traversing a
I have sort of a philosophical question, which also need to consider the performance
Consider the following nonsense script as an example: use strict; use warnings; my $uninitialisedValue;
I support SQL Servers 2000 thru 2008R2 (all Standard Edition). Question 1 Please consider

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.