Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

std::function has no clear() member?

This topic is 2542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

std::function does not appear to have a function for clearing its contents. Why is this? I was under the impression that std::function, std::shared_ptr, etc where more or less ports of the Boost versions, but they appear to be missing boost features. What is the recommended process for porting from boost to c++11? It seems like a simple search and replace isn't going to work.

Share this post


Link to post
Share on other sites
Advertisement
Well doing func = 0; does work, but I find func.clear() to be more readable and less error prone. And my compiler doesn't support nullptr.

Also, std::bind appears to work differently as well. Right now I'm getting an error about _1 not being declared in scope. Shame there's no documentation...

Share this post


Link to post
Share on other sites
Hidden
P.S., how do you edit posts? This new forum design is ridiculous.

Anyway, std::bind appears to work differently as well. Right now I'm getting an error about _1 not being declared in scope. Shame there's no documentation...

Share this post


Link to post
mingw with gcc 4.4.1
Or more accurately whatever came with Code::Blocks 10.05

By the way, here is a minimal example that fails to compile.
[source lang='cpp']#include <functional>

int foo(int x, int y){
return 0;
}

void bar(){
std::function<int (int)> f = std::bind(foo, 42, _1);
}
[/source]

Compile errors

C:\CodeBlocks\GeneratorFinder\test.cpp||In function 'void bar()':|
C:\CodeBlocks\GeneratorFinder\test.cpp|8|error: '_1' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|

Share this post


Link to post
Share on other sites
_1 lives in either namespace std::tr1::placeholders or std::placeholders, depending on TR1 vs C++11 support for your compiler.

Share this post


Link to post
Share on other sites
Why does that not show up in any usage examples? Is there a hidden using std::placeholders somewhere?

Why is Boost able to work without explicit namespace qualification on the placeholders while the standard library isn't?

Share this post


Link to post
Share on other sites
You may not be aware of this, but there is more than one source of usage examples. I don't know what usage examples you are looking at, so I can't comment on the quality, reasoning or techniques behind them.

Boost places _1 and friends in an anonymous namespace in the root namespace. They don't exist in the boost namespace.

Share this post


Link to post
Share on other sites
Thanks, I didn't know that Boost was using an unamed namespace. I gues the equivalent would be putting namespace { using namespace std::placeholders; } in your code.

Incidently, it looks like Boost is considering getting rid of that system
https://svn.boost.or...ost/ticket/2240



Edit: I found another difference. std::bind appears unable to handle taking the address of an overloaded member function, which boost did just fine. I got around it by creating a temporary function pointer with the desired type, but I am curious why the difference exists.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!