What does the C++ error “A pointer to a bound function may only be used to call the function” mean?

Raymond Chen

TL;DR: You got all set to call a member function but forgot to call it.

void oops(std::vector<std::string>& v)
{
    set_name(v.front.c_str());
    //         ^^^^^ error: A pointer to a bound function
    //         may only be used to call the function
}

What you meant was

void oops(std::vector<std::string>& v)
{
    set_name(v.front().c_str());
    //              ^^
}

Here’s how to decode the error message:

A “bound function” is

  • An object, a dot, and a member function name, or
  • A pointer, a -> arrow, and a member function name.

In the above example, it is the v.front.

Once you write v.front, the only thing you can do next is follow up with parentheses to call the member function. If you forget the parentheses, then you get an error from the compiler that’s basically saying, “The only thing you are allowed to do is use parentheses to call the function, but you didn’t do that!”

Usually it’s because you got all set up to the call the function, and then got distracted and forgot to call it.

So call the function. That’s what you wanted to do in the first place. You just forgot to do it.

Bonus chatter: Many years ago, I was buying tickets at a ticket office (back when you bought tickets at a ticket office), and I got so distracted by the process of buying and paying for tickets that I left the counter without my tickets! Fortunately, I had gotten only a few steps away, and the tickets were waiting for me when I turned around and went back.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • Ray Koopa 0

    Or you switch between C# and C++ way too often and accidentally mistake this method with a property and try to get it like one.

  • BCS 0

    If you *really* want to refer to a bound function without “calling” it yet (as in you need an expression that is callable, but in a context where you don’t just call it right away), you can use a lambda:

    [&v](auto …p){ return v.front(p…); }

    Note that using a generic lambda also has the advantage of deferring overload resolution until the context where the resulting expression is called. (Also, this should probably use perfect forwarding, but that would make it too hard to read for this context.)

  • Falcon 0

    This article reminded me of “Toast created but not shown: did you forget to call show()?”

    Yes, this is from first-hand experience!

Feedback usabilla icon