English 中文(简体)
Uniform initialization in C++0x, when to use () instead of {}?
原标题:
  • 时间:2009-12-08 00:18:21
  •  标签:
  • c++
  • c++11

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

To initialize a struct:

struct myclass
{
    myclass(int px, int py) : x(px), y(py) {}
private:
    int x, y;
};
...
myclass object{0, 0};

Now in the case of a vector for example, it has many constructors. Whenever I do the following:

vector<double> numbers{10};

I get a vector of 1 element instead of one with 10 elements as one of the constructors is:

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

My suspicion is that whenever a class defines an initializer list constructor as in the case of a vector, it gets called with the {} syntax.

So, is what I am thinking correct. i.e. Should I revert to the old syntax only whenever a class defines an initializer list constructor to call a different constructor? e.g. to correct the above code:

vector<double> numbers(10); // 10 elements instead of just one element with value=10
问题回答

I ve found the answer in the standard docs(latest draft). Hopefully, I ll try to explain what I understood.

First, if a class defines an initialization list constructor, then it is used whenever suitable:

§ 8.5.4 (page 203)

Initializer-list constructors are favored over other constructors in list-initialization (13.3.1.7).

I think this is a great feature to have, eliminating the headache associated with the non-uniform style :)

Anyway, the only gotcha(which my question is about) is that if you design a class without the initializer constructor, then you add it later you may get surprising result.

Basically, imagine std::vector didn t have the initializer list constructor, then the following would create a vector with 10 elements:

std::vector<int> numbers{10};

By adding the initializer list constructor, the compiler would favor it over the other constructor because of the {} syntax. This behavior would happen because the elements of the init-list {10} are accepted using the init-list constructor. If there is no acceptable conversion, any other constructor shall be used e.g.:

std::vector<string> vec{10};
// a vector of 10 elements.
// the usual constructor got used because "{0}"
// is not accepted as an init-list of type string.

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签