0

I'm rather a beginner in C++ and yesterday I came across a constructor I don't understand.

struct Person
{
    explicit Person(std::vector<std::function<void (float)>> &update_loop)
    {
        update_loop.emplace_back([this](float dt) { update(dt); });
    }

    void update(float dt);
};

int main()
{
    std::vector<std::function<void (float)>> update_loop;

    Person person { update_loop };
}

I know it's possible to initialize aggregate objects in this way, but Person doesn't have data members, just a constructor and a function, so what exactly Person person { update_loop } does? If it uses the explicit constructor shouldn't it use round parentheses, like Person person(update_loop) ?

Wojtek
  • 801
  • 1
  • 9
  • 13
  • BTW if there is a user-provided constructor then a class is no longer an aggregate; data members or not – M.M Sep 12 '14 at 07:32

1 Answers1

1

The syntax here

Person person { update_loop };

Is the new "uniform initialization" syntax (introduced in C++11) and is part of the general direct initialization.

In your case, using the () or the {} would be equivalent, but this is not always the case. {} does not allow narrowing conversions etc. and it also avoids the "most vexing parse" issues found with ().

Niall
  • 30,036
  • 10
  • 99
  • 142