2

INFO

I'd like to use boost::function to pass callback as parameter, like this way:

void ReadPacket(
        boost::function<void (const boost::system::error_code&, Packet* p)> callback);

and then use it :

ReadPacket(boost::bind(
    &ServerSession::storePacket,
    this,
    _1,
    _2
    ));

After all after a chain of callbacks i call

callback(ec, packet);

PROBLEM

I've just compiled solution in Debug and everything looks OK ...

but in Release I got lots of errors mentioned above

BasicSession.h(30): error C2039: 'function' : is not a member of 'boost'
BasicSession.h(30): error C2061: syntax error : identifier 'function'
BasicSession.h(30): error C2059: syntax error : ')'
BasicSession.h(30): error C2143: syntax error : missing ')' before ';'

I'm confused and dissappointed.

SUGGESTION

I've found that there're different syntax in boost::function. For example boost::function0 or boost::function1. This was made due to VS2010 doesn't support something (I don't know what exactly)

Am I right?

I also need to make this application as portable and cross-platform as possible.

boost 1.47 and VS2010

nix
  • 464
  • 1
  • 5
  • 13
  • MSVC10 does not support *variadic templates*. Anything that fakes variadic behaviour (such as `bind`) must employ some dirty trickery under the hood. – Kerrek SB Nov 17 '11 at 16:38
  • @kerrekSB so...what should I do if I'd like to use methods passing callback function as a parameter. Like done in ASIO. – nix Nov 17 '11 at 16:44
  • 1
    No idea, sorry :-) Sounds like it's an MSVC-specific problem. I'm sure someone with experience with that compiler will post something useful soon! – Kerrek SB Nov 17 '11 at 16:45

2 Answers2

3

In case if you have missed it, here is the tutorial on boost function. If you look at the tutorial it will list both the 'preferred' and the 'portable' syntax. Since you want the code to be portable, you might want to choose the latter.

yasouser
  • 5,113
  • 2
  • 27
  • 41
  • thx. I found it already. And this is the answer? just using function1? – nix Nov 17 '11 at 16:45
  • But that tutorial says that Visual C++ 7.1 and higher *do* support the "preferred" syntax. – ruakh Nov 17 '11 at 16:45
  • @ruakh Agree with you. also strange that code above do be compiled in Degug but do not in Release – nix Nov 17 '11 at 20:08
2

the solution is boost::function4

Roman Kr
  • 80
  • 2
  • 1
    Welcome on SO :) In general we expect an answer to not only provide the solution but also explain what the problem is. This may mean lengthy answers... but that's what allow people to educate themselves so they won't *need* SO the next time. – Matthieu M. Nov 17 '11 at 16:46
  • @RomanKr I like this short answer, but, 1. Why it compiles in VS2010 in Debug and do not in Release. and said: tutorial says that Visual C++ 7.1 and higher do support the "preferred" syntax. What the point ??? – nix Nov 17 '11 at 20:10
  • it works for me now using boost::functionX. but it is still mysterious secret why MSVC compile the version above in Debug but DO NOT in Release. – nix Nov 17 '11 at 23:18
  • @nix: you may wish to bring this to the attention of Microsoft and report the bug :) With hope, they'll answer you. – Matthieu M. Nov 18 '11 at 07:15