Re: std::function and rvalues
On 2011-11-01 13:34, demo wrote:
Hi,
Should the following compile in C++11?
#include<string>
#include<iostream>
#include<functional>
void foo(std::string&& i)
{
std::cout<< i<< std::endl;
}
int main(int argc, char* argv[])
{
std::function<void(std::string&&)> func =&foo;
}
I think the code is well-formed and well-defined. The std::function
instantiation shall be callable for argument types std::string&&
and return type void and this is what you function foo seems to be,
because the definition is based on whether the expression
INVOKE(f, declval<std::string&&>(), void)
is well-formed (as unevaluated operand), where f is an lvalue of void
(*)(std::string&&) in this example. Now lets look at the definition of
the semantics of INVOKE as defined in [func.require] p1+2:
a) Based on p2 an expression that evaluates to void can be implicitly
converted to void. This brings us to what the semantics is of:
b) INVOKE(f, declval<std::string&&>())
which is described in the decision ladder of p1. Of all bullets only the
last one applies, which effectively requires that the expression
f(declval<std::string&&>())
shall be well-formed (as unevaluated operand). Translated into code this
means that
#include <utility>
#include <string>
typedef void(*F)(std::string&&);
typedef
decltype(std::declval<F&>()(std::declval<std::string&&>()))
type;
shall be well-formed, where std::declval<F&>() corresponds to the lvalue
f mentioned above. This seems to be the case here.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]