Talk:cpp/utility/functional/function
Contents |
[edit] Incomplete Sentence - help needed
This sentence is incomplete: "Care should be taken when a std::function whose result type is a reference initialized from a lambda expression without a trailing-return-type." The entire "whose result is a reference..." clause is just modifying "a std::function", so it reads as "Care should be taken when a std::function whose x is y." I believe there should be an "is" between "reference" and "initialized", like so: "Care should be taken when a std::function whose result type is a reference is initialized from a lambda expression without a trailing-return-type." Can anyone confirm? Kslattery (talk) 10:53, 9 June 2022 (PDT)
- OK, updated. Thanks. :) --Space Mission (talk) 11:42, 9 June 2022 (PDT)
[edit] std::function as function argument
I think there should also be an example for using a std::function as function argument. Especially when using that with a functor (i.e. a class with operator()) with std::function where the content of the functor is modified by the function call and should be retrievable later.
As an example:
#include<functional> #include<iostream> void pass_on_value(int value, std::function<void(int)> const & func) { func(value); } struct Sum { int value = 0; void operator()(int i) { value += i; } }; int main() { std::function<void(int)> sum = Sum; pass_on_value(5, sum); pass_on_value(3, sum); Sum* p = sum.target<Sum>(); std::cout << p->value << std::endl; // "correctly" gives 8 Sum other; pass_on_value(5, other); pass_on_value(3, other); std::cout << other.value << std::endl; // prints 0 }
188.104.155.126 12:24, 2 February 2017 (PST) Michael
- generally top-level pages for classes try to show the most general use cases, if at all. I'd even say the example on std::function's top-level page is more suitable for the constructor (std::function::function, which has no example now), but people have found it useful - I've seen it mentioned on the internet and at work as "look what you can do with std::function", so it seems to be good where it is. I agree that passing an std::function as a function parameter would be a good general use case, if we show where it is better than a directly callable template parameter. A stateful function object, while cool, is a rather specific use of std::function. I think it is actually a good demo of std::function::target (current example there compares the target with known targets, but doesn't actually use the target's state). --Cubbi (talk) 13:04, 2 February 2017 (PST)
[edit] In the section "Non member functions"
nullptr is not declared in the std namespace.
[edit] example code compilation problem
While the example code compiles fine using g++ (4.8.0) and clang++ (3.2) on my linux platform, it does not seem to compile using Visual Studio 2012, using either msvc++ or Intel C++ 13.1. The error I get can be found at the end of my post (sorry for the monster I pasted, but template errors often are just that). It seems to be an error related to the 'functional' header on windows.
1> main.cpp 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(516): error C2664: 'std::_Func_class<_Ret,_V0_t,_V1_t>::_Set' : cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx,_V0_t,_V1_t> *' 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int 1> ] 1> and 1> [ 1> _Rx=void, 1> _V0_t=const Foo &, 1> _V1_t=int 1> ] 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(516) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t,_V1_t>::_Do_alloc<_Myimpl,_Fret(__thiscall Foo::* const &)(int) const,_Alloc>(_Fty,_Alloc)' being compiled 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int, 1> _Fret=void, 1> _Alloc=std::allocator<std::_Func_class<void,const Foo &,int>>, 1> _Fty=void (__thiscall Foo::* const &)(int) const 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(516) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t,_V1_t>::_Do_alloc<_Myimpl,_Fret(__thiscall Foo::* const &)(int) const,_Alloc>(_Fty,_Alloc)' being compiled 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int, 1> _Fret=void, 1> _Alloc=std::allocator<std::_Func_class<void,const Foo &,int>>, 1> _Fty=void (__thiscall Foo::* const &)(int) const 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(516) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t,_V1_t>::_Reset_alloc<_Fret,Foo,int,std::allocator<_Ty>>(_Fret (__thiscall Foo::* const )(int) const,_Alloc)' being compiled 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int, 1> _Fret=void, 1> _Ty=std::_Func_class<void,const Foo &,int>, 1> _Alloc=std::allocator<std::_Func_class<void,const Foo &,int>> 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(516) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t,_V1_t>::_Reset_alloc<_Fret,Foo,int,std::allocator<_Ty>>(_Fret (__thiscall Foo::* const )(int) const,_Alloc)' being compiled 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int, 1> _Fret=void, 1> _Ty=std::_Func_class<void,const Foo &,int>, 1> _Alloc=std::allocator<std::_Func_class<void,const Foo &,int>> 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t,_V1_t>::_Reset<void,Foo,int>(_Fret (__thiscall Foo::* const )(int) const)' being compiled 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int, 1> _Fret=void 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret,_V0_t,_V1_t>::_Reset<void,Foo,int>(_Fret (__thiscall Foo::* const )(int) const)' being compiled 1> with 1> [ 1> _Ret=void, 1> _V0_t=const Foo &, 1> _V1_t=int, 1> _Fret=void 1> ] 1> c:\users\robbe\documents\projects\maxwell\src\scratch\main.cpp(30) : see reference to function template instantiation 'std::function<_Fty>::function<void(__thiscall Foo::* )(int) const>(_Fx &&)' being compiled 1> with 1> [ 1> _Fty=void (const Foo &,int), 1> _Fx=void (__thiscall Foo::* )(int) const 1> ] 1> c:\users\robbe\documents\projects\maxwell\src\scratch\main.cpp(30) : see reference to function template instantiation 'std::function<_Fty>::function<void(__thiscall Foo::* )(int) const>(_Fx &&)' being compiled 1> with 1> [ 1> _Fty=void (const Foo &,int), 1> _Fx=void (__thiscall Foo::* )(int) const 1> ] 1> 1>Build FAILED.
- Documenting the bugs of individual compilers or libraries is generally beyond the scope of the wiki. Although major deficiencies sometimes appear in the "Notes" sections, it's hard to maintain such lists as compiler vendors keep releasing new versions. If you want to use equivalent functionality on a Windows platform, I recommend the boost.Function library]. --Cubbi 12:16, 7 April 2013 (PDT)
- Thank you for the clarification. My intent was not to document platform deficiencies but to make sure the problem did not lie with the code.