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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:37:01+00:00 2026-06-05T19:37:01+00:00

I have a C++ expression that I wish to ‘freeze’. By this, I mean

  • 0

I have a C++ expression that I wish to ‘freeze’. By this, I mean I have syntax like the following:

take x*x with x in container ...

where the ... indicates further (non-useful to this problem) syntax. However, if I attempt to compile this, no matter what preprocessor translations I’ve used to make ‘take’ an ‘operator’ (in inverted commas because it’s technically not an operator, but the translation phase turns it into a class with, say, operator* available to it), the compiler still attempts to evaluate / work out where the x*x is coming from, (and, since it hasn’t been declared previously (as it’s declared further at the ‘in’ stage), it instead) can’t find it and throws a compile error.

My current idea essentially involves attempting to place the expression inside a lambda (and since we can deduce the type of the container, we can declare x with the right type as, say, [](decltype(*begin(container)) x) { return x*x } — thus, when the compiler looks at this statement, it’s valid and no error is thrown), however, I’m running into errors actually achieving this.

Thus, my question is:
Is there a way / what’s the best way to ‘freeze’ the x*x part of my expression?

EDIT:
In an attempt to clarify my question, take the following. Assume that the operator- is defined in a sane way so that the following attempts to achieve what the above take ... syntax does:

MyTakeClass() - x*x - MyWithClass() - x - MyInClass() - container ...

When this statement is compiled, the compiler will throw an error; x is not declared so x*x makes no sense (nor does x – MyInClass(), etc, etc). What I’m trying to achieve is to find a way to make the above expression compile, using any voodoo magic available, without knowing the type of x (or, in fact, that it will be named x; it could viably be named ‘somestupidvariablename’) in advance.

  • 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-05T19:37:02+00:00Added an answer on June 5, 2026 at 7:37 pm

    I made an answer very similar to my previous answer, but using actual expression templates, which should be much faster. Unfortunately, MSVC10 crashes when it attempts to compile this, but MSVC11, GCC 4.7.0 and Clang 3.2 all compile and run it just fine. (All other versions untested)

    Here’s the usage of the templates. Implementation code is here.

    #define take 
    #define with ,
    #define in >>= 
    
    //function call for containers 
    template<class lhsexpr, class container>
    lhsexpr operator>>=(lhsexpr lhs, container& rhs)
    {
        for(auto it=rhs.begin(); it!=rhs.end(); ++it)
            *it = lhs(*it);
        return lhs;
    }
    
    int main() {
        std::vector<int> container0;
        container0.push_back(-4);
        container0.push_back(0);
        container0.push_back(3);
        take x*x with x in container0; //here's the magic line
        for(auto it=container0.begin(); it!=container0.end(); ++it)
            std::cout << *it << ' ';
        std::cout << '\n';
    
        auto a = x+x*x+'a'*x;
        auto b = a; //make sure copies work
        b in container0;
        b in container1;
        std::cout << sizeof(b);
    
        return 0;
    }
    

    As you can see, this is used exactly like my previous code, except now all the functions are decided at compile time, which means this will have exactly the same speed as a lambda. In fact, C++11 lambdas were preceeded by boost::lambda which works on very similar concepts.

    This is a separate answer, because the code is far different, and far more complicated/intimidating. That’s also why the implementation is not in the answer itself.

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

Sidebar

Related Questions

I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
I have an interface contract that looks like this: ICollection<FooBar> FindByPredicate(Expression<Func<FooBar,bool>> predicate); ICollection<Foo> FindByPredicate(Expression<Func<Foo,bool>>
I have the following expression that should match the entire given word in case
I have the following regular expression that works fine in perl: Classification:\s([^\n]+?)(?:\sRange:\s([^\n]+?))*(?:\sStructural Integrity:\s([^\n]+))*\n The
I have the following LINQ expression that's not returning the appropriate response var query
I have been thinking about a regular expression that can transform a list like
I have an expression that I want to modify. This expression, for the most
I have a Linq expression that (for northwind) gets the total qty ordered per
I have a regex expression that I'm doing a split against another string and
How can I have a regular expression that tests for spaces or tabs, but

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.