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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:45:14+00:00 2026-05-15T01:45:14+00:00

Boost lambda allows to overwrite deduced return type using ret<T> template. I have tried

  • 0

Boost lambda allows to overwrite deduced return type using ret<T> template.
I have tried searching for equivalent in phoenix but could not find one.

Is there an equivalent in phoenix? I know how to make my own Replacement but I would rather not. thank you

  • 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-15T01:45:15+00:00Added an answer on May 15, 2026 at 1:45 am

    Rewrite: I missed the point on my first answer (it was late), let me try again.

    Let me give some exposition for people like me who might miss your point the first time. In boost::lambda, when using user defined types in operator expressions, one has to use the ret<> function to override return type deduction. This is because the lambda return type deduction system only supports native (and stl? I don’t remember) types directly. A short example:

    using namespace boost::lambda;
    
    struct add_t{
        add_t(int i) : i(i) {};
        add_t operator+(const add_t& other) const{
            return add_t(i + other.i);
        }
        int i;
    };
    
    (_1 + _2)(add_t(38), add_t(4));           // RETURN TYPE DEDUCTION FAILS
    ret<add_t>(_1 + _2)(add_t(38), add_t(4)); // OK
    

    In phoenix though, no hints are are needed (note that literals and non-const temporaries cannot appear in a phoenix argument list):

    using namespace boost::phoenix;
    
    add_t i(38), j(4);
    (_1 + _2)(i, j);    // JUST FINE
    

    The return type deduction system is completely different and much more natural in phoenix; it will properly deduce the return type of operators that use conventional semantics. Specifically, the return type should match the type of one of the operands, be a reference, pointer, or const pointer to one of the argument types, or be an stl container/container iterator of one of those types. There is a nice write up of phoenix return type deduction in type_deduction.hpp header for more details.

    So now I’m reading your question as, how can non-conventional operator semantics be handled in phoenix?

    Consider the following strange pair of types as an example

    struct add_ret_t{
        add_ret_t(int i) : i(i) {};
        int i;
    };
    
    struct add_t{
        add_t(int i) : i(i) {};
        add_ret_t operator+(const add_t& other) const{
            return add_ret_t(i + other.i);
        }
        int i;
    };
    

    For lambda, this is no problem, just use the ret function:

    using namespace boost::lambda;
    
    ret<add_ret_t>(_1 + _2)(add_t(38), add_t(4)); // OK
    

    But phoenix can’t deal with this operator (can you blame it?) because the return type is not related to the arguments, and there is no way to directly indicate the return type in phoenix. If there is a good reason to use an operator like this, a case could be added to the type deduction system, but I can’t see a way to do this without hacking type_deduction.hpp or branching a good portion of phoenix.

    Alternatively, I figured out a little hack to override the return types for specific operators. The result_of_operation template structures in boost/spirit/home/phoenix/operator/arithmetic.hpp (lines 39-56 list the struct types, boost 1.43) execute the type deduction when they are instantiated and store the result. So all that is needed is to provide some template specializations for the problem operations, which only need contain one typedef specifying the return type. Example (codepad for full src):

    using namespace boost::phoenix;
    
    namespace boost{ namespace phoenix{
    
    //override add_t addition to give add_ret_t
    template <> struct result_of_plus<add_t&, add_t&> { typedef add_ret_t type; };
    
    //override int addition to give char
    template <> struct result_of_plus<int&, int&> { typedef char type; };
    
    }}
    
    int main()
    {
        add_t i = 1, j = 7;
        std::cout << ((_1 + _2)(i, j)).i << std::endl;
    
        int k = 51, l = 37;
        std::cout << ((_1 + _2)(k, l)) << std::endl;
    
        return 0;
    }
    

    This certainly isn’t a ret replacement, but in some senses its better since its global. If there are a lot of operators to overload, the full set of operations could be macroed.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

There are a lot of impressive Boost libraries such as Boost.Lambda or Boost.Phoenix which
I am using boost::lambda to remove subsequent whitespaces in a string, leaving only one
I've recently started using boost lambda and thought I'd try and use it in
Is it possible to create an inline lambda using boost which always throws an
I have tried to write my first Boost program from information on the Boost
I'm using boost::iostreams::gzip_decompressor with boost::iostreams::filterimg_streambuf to read gzip files. some of my files have
I am learning the boost::lambda library and for that I wrote this sample code
Why is this boost::lambda expression not working? boost::function<bool (boost::uint64_t, boost::uint64_t&, unsigned int, float)> myFunct
I'm looking at boost::lambda as a way to to make a generic algorithm that
I can't seem to find boost::lambda::ll for a nested ll::for_each() invocations in any header

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.