public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/59182] New: can't convert from void (T::*)() to std::function<void()>
@ 2013-11-18 23:16 sequoiahead at gmail dot com
2013-11-19 0:12 ` [Bug libstdc++/59182] " redi at gcc dot gnu.org
0 siblings, 1 reply; 2+ messages in thread
From: sequoiahead at gmail dot com @ 2013-11-18 23:16 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59182
Bug ID: 59182
Summary: can't convert from void (T::*)() to
std::function<void()>
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: sequoiahead at gmail dot com
Created attachment 31242
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=31242&action=edit
gcc -v output
I'm trying to compile following piece of code:
#include <functional>
struct Func {
void func() {}
void pfunc(int p) {}
};
int main(int argc, char** argv) {
Func fInstance;
std::function<void(int)> voidMemberParamFunc = std::bind(&Func::pfunc,
fInstance, std::placeholders::_1);
std::function<void()> voidMemberFunc = std::bind(&Func::func, fInstance,
std::placeholders::_1); //compilation fails here
}
via "g++ -MMD -MP -D_DEBUG -Werror -Wall -g -std=c++11 -fno-rtti -o "main.o" -c
"main.cpp"
and receive following error:
main.cpp:24:96: error: conversion from 'std::_Bind_helper<false, void
(Func::*)(), Func&, const std::_Placeholder<1>&>::type {aka
std::_Bind<std::_Mem_fn<void (Func::*)()>(Func, std::_Placeholder<1>)>}' to
non-scalar type 'std::function<void()>' requested
std::function<void()> voidMemberFunc = std::bind(&Func::func, fInstance,
std::placeholders::_1); //compilation fails here
The problem is that the compiler is able to convert void Func::pfunc(int) to
std::function<void(int)>, but can't convert void Func::func() to
std::function<void()>
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug libstdc++/59182] can't convert from void (T::*)() to std::function<void()>
2013-11-18 23:16 [Bug libstdc++/59182] New: can't convert from void (T::*)() to std::function<void()> sequoiahead at gmail dot com
@ 2013-11-19 0:12 ` redi at gcc dot gnu.org
0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2013-11-19 0:12 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59182
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to sequoiahead from comment #0)
> The problem is that the compiler is able to convert void Func::pfunc(int) to
> std::function<void(int)>, but can't convert void Func::func() to
> std::function<void()>
No, that's not what's happening.
std::bind(&Func::pfunc, fInstance, std::placeholders::_1) returns a function
object, call it B1, that takes a single argument, as indicated by the
placeholder. B1 is convertible to function<void(int)>, because that type takes
also a single argument, so will pass its argument to B1.
B1 is not convertible to function<void()> because you have to call a
function<void()> with no arguments, and B1 cannot be called with no arguments.
std::function is rejecting your program because it's invalid. std::bind
doesn't do the same checking, so doesn't reject it, but your program is still
invalid.
This compiles OK:
std::function<void()> voidMemberFunc = std::bind(&Func::func, fInstance);
This bind expression creates a function object that takes no arguments, so can
be stored in a function<void()>.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-11-19 0:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-18 23:16 [Bug libstdc++/59182] New: can't convert from void (T::*)() to std::function<void()> sequoiahead at gmail dot com
2013-11-19 0:12 ` [Bug libstdc++/59182] " redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).