std::function has no clear() member?
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.
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...
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...
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...
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...
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
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 ===|
_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?
Why is Boost able to work without explicit namespace qualification on the placeholders while the standard library isn't?
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement