1

This code

#include<functional>

int foo(char);

int main()
{
    std::function<decltype((foo))> f;
}

gives the error:

aggregate ‘std::function<int (&)(char)> f’ has incomplete type
and cannot be defined

in gcc-4.7.2. I thought it would compile.

I know that I could drop the additional parenthesis to make it work, but I am trying to understand what is happening.

I do not understand why decltype adds a reference. I would think that a function name in parenthesis is a prvalue and thus that decltype deduces the type without adding a reference.

levzettelin
  • 2,600
  • 19
  • 32
  • Also, function has no value per se and its name cannot be converted to an rvalue, so is certainly not a prvalue. – Potatoswatter Jul 20 '13 at 12:57
  • The expression `foo` is an `lvalue` of type `function of (char) returning int`. `decltype((expr))` converts an `lvalue` of type `T` to `lvalue-reference to T`, hence the output type `int (&)(char)` or `lvalue-reference to function of (char) returning int`. – Andrew Tomazos Jul 20 '13 at 16:47
  • Ohh, this answers my question. I though that foo was an prvalue, because you cannot assign to it. But, yeah, it has a name, you can take its address. So it is of course an lvalue. Thanks. – levzettelin Jul 26 '13 at 09:00

0 Answers0