Boost.Function compile error

Dear all, i got a compilation error with vstudio 2010 and Boost.Function: 1) pass functor in constructor 2) pass functor as assignment 3) use Boost.Ref 4) temporary function void foo() { std::identity<int> bla; boost::function<int (int)> fc(bla); boost::function<int (int)> fc2 = bla; boost::function<int (int)> fc(boost::ref(bla)); boost::function<int (int)>(bla); //<-- } first 3 are ok, but the last one vstudio sees this as redefintion of bla of type 'boost::function<int (int)>. What (and why) do I wrong or should use an explicit cast in the last case?

AMDG On 03/06/2013 03:06 PM, gast128 wrote:
The last line is treated the same as: boost::function<int(int)> bla; You can get around this by using it in a larger expression or just by wrapping it in parentheses, to prevent it from being parsed as a declaration. i.e. (boost::function<int (int)>(bla)); In Christ, Steven Watanabe

thx, I got a simpeler case without Boost.Function: struct Bla { template <typename T> Bla(T t){ t; } }; void Test() { std::identity<int> id; Bla(id); //<- } From curiosity (and not a Boost.Function question anymore), which c++ rule kicks on to treat as a declaration?

On 3/7/2013 9:00 AM, Igor R wrote:
Thx, I was aware of Scott Meyers item (stumbled upon that already in the past), but here it can't be seen as a function since 'id' is not a type but a variable. The compiler also see 'id' not as a function but as a variable declaration of 'Bla'. Is that because u can also declare a variable like this: int (i) = 0; and why is that allowed?

I belive this falls under the paragraph 6.8: "There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion (5.2.3) as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those cases the statement is a declaration." While paragraph 5.1 (5) says: "A parenthesized expression is a primary expression whose type and value are identical to those of the enclosed expression. The presence of parentheses does not affect whether the expression is an lvalue. The parenthesized expression can be used in exactly the same contexts as those where the enclosed expression can be used, and with the same meaning, except as otherwise indicated." Try the following :) int (main()) { }
participants (3)
-
gast128
-
Igor R
-
Steven Watanabe