Uniform initialization in C++0x, when to use () instead of {}?

前端 未结 2 944
时光取名叫无心
时光取名叫无心 2021-02-13 13:17

Is there a rule of thumb to decide when to use the old syntax () instead of the new syntax {}?

To initialize

2条回答
  •  面向向阳花
    2021-02-13 13:47

    Take a look at this:

    http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=453&rll=1

    The use of a {}-style initializers on a variable has no direct mapping to the initialization lists on any constructors of the class. Those constructor initialization lists can be added/removed/modified without breaking existing callers.

    Basically the different behavior of the container is special, and requires special code in that container, specifically a constructor taking a std::initializer_list. For POD and simple objects, you can use {} and () interchangeably.

提交回复
热议问题