September 26th, 2022

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

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

3 comments

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

  • Falcon

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

    Yes, this is from first-hand experience!

  • BCS

    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...

    Read more
  • Ray Koopa

    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.