std::function has no clear() member?

Started by
7 comments, last by Storyyeller 12 years, 3 months ago
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.
I trust exceptions about as far as I can throw them.
Advertisement
func = nullptr;

? just a guess
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...
I trust exceptions about as far as I can throw them.
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...
I trust exceptions about as far as I can throw them.
What compiler are you using?
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 ===|
I trust exceptions about as far as I can throw them.
_1 lives in either namespace std::tr1::placeholders or std::placeholders, depending on TR1 vs C++11 support for your compiler.
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?
I trust exceptions about as far as I can throw them.
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.
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.
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement