yi chen
yi chen

Reputation: 241

Boost phoenix value and function return value type

I'm very new to Boost::Phoenix and I'm using it to do FP in C++. I went though the tutorial on their official pages. However, I'm wondering why no examples show how to "Save" the variables. For example, in the values example it says to use a function variable

std::cout << val(3)() << std::endl;

it directly prints out the executed result. What if I want to save the variable? like

type t = val(3);

What is the type of val(3)? Same thing happens when I want to declare the type of a function variable returned by a lazy function. What is the type of it? I don't understand why the entire tutorial always output it immediately. Am I using it wrong?

Thanks, Yi

Upvotes: 2

Views: 506

Answers (1)

alfC
alfC

Reputation: 16242

You can always (demangle) typeid(...phoneix expression...).name() (or create a compiler error) to see the type of the expression. Soon you will realize that you are not meant (and is not practical) to know the type that represents the expression (they are tens of lines long in some cases).

So answering your first question:

typeid(boost::phoenix::val(3.) =
boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<double>, 0l> >

Everything inside actor is an implementation detail that you should not rely on.

In C++11 you can use auto but since all you want to know is the function aspect of it you can do almost achieve the same thing by storing the expression as boost::function (now std::function). For example:

auto f1 = boost::phoenix::val(3.);
std::function<double()> f2 = boost::phoenix::val(3.);

Then

f1()

gives 3.

f2()

also gives 3.

Answering your second question, if you need to know the type of the expression your are using the library in the wrong way in my opinion, because that is an implementation detail (in fact it changed in different version of Phoenix).

Upvotes: 3

Related Questions