value-initialization

value initialization for automatic variables [duplicate]

孤者浪人 提交于 2020-01-25 00:08:25
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: non-copyable objects and value initialization: g++ vs msvc Value-initializing an automatic object? Consider the following statement: It's not really possible to value-initialize an automatic object. Is this statement true? I see no problem in doing this: int main() { int i = int(); } 回答1: The term value-initialization is defined in 8.5 [dcl.init] paragraph 16, 4th bullet: If the initializer is (), the object is

How Do Zero-Initialization, Static-Initialization, and Value-Initialization Differ?

半腔热情 提交于 2020-01-03 10:47:11
问题 Ben Voigt has pointed out here that: Zero initialization is one of the steps of static initialization. But you're right that you can't blindly substitute the latter (tag), since zero initialization is also performed for value initialization. However, there's no need for (a tag named) zero-initialization in the context of C++, because tags already exist for both static initialization and value initialization, and those are more relevant. I thought there was a case where it made sense to "Zero

non-copyable objects and value initialization: g++ vs msvc

醉酒当歌 提交于 2019-12-28 18:38:12
问题 I'm seeing some different behavior between g++ and msvc around value initializing non-copyable objects. Consider a class that is non-copyable: class noncopyable_base { public: noncopyable_base() {} private: noncopyable_base(const noncopyable_base &); noncopyable_base &operator=(const noncopyable_base &); }; class noncopyable : private noncopyable_base { public: noncopyable() : x_(0) {} noncopyable(int x) : x_(x) {} private: int x_; }; and a template that uses value initialization so that the

The behavior of value-initializing an enum

此生再无相见时 提交于 2019-12-20 09:28:09
问题 First, I want to say, according to cppreference.com, it is somewhat impossible to value-initialize an enum. According to http://en.cppreference.com/w/cpp/language/value_initialization, value-initializing an enum actually performs zero-initialization. It then follows that, according to http://en.cppreference.com/w/cpp/language/zero_initialization, the effect of zero-initializing an enum is: If T is a scalar type, the object's initial value is the integral constant zero implicitly converted to

Explicit Type Conversion and Multiple Simple Type Specifiers

霸气de小男生 提交于 2019-12-17 12:11:11
问题 To value initialize an object of type T , one would do something along the lines of one of the following: T x = T(); T x((T())); My question concerns types specified by a combination of simple type specifiers, e.g., unsigned int : unsigned int x = unsigned int(); unsigned int x((unsigned int())); Visual C++ 2008 and Intel C++ Compiler 11.1 accept both of these without warnings; Comeau 4.3.10.1b2 and g++ 3.4.5 (which is, admittedly, not particularly recent) do not. According to the C++

Do we need an accessible copy constructor for value initialization in C++98/03?

时间秒杀一切 提交于 2019-12-13 16:16:43
问题 This question refers only to pre C++11 . Consider the following seemingly broken code: struct X { X(){} // default user-provided constructor private: X(const X&){} }; int main() { X x = X(); } Live on Coliru According to cppreference.com in pre C++11 the default ctor will be called: The effects of value initialization are: 1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called; ... This seem to imply that the copy ctor doesn't

Why is this simple assignment undefined behaviour?

喜夏-厌秋 提交于 2019-12-12 10:30:19
问题 I was refreshing my understanding of value-initialisation versus default-initialisation, and came across this: struct C { int x; int y; C () { } }; int main () { C c = C (); } Apparently this is UB because In the case of C(), there is a constructor that is capable of initializing the x and y members, so no initialization takes place. Attempting to copy C() to c therefore results in undefined behavior. I think I understand why, but I'm not certain. Can someone please elaborate? Does that mean

Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard?

断了今生、忘了曾经 提交于 2019-12-08 15:15:14
问题 Cheers and hth. - Alf made a comment in this answer that value initialization is arguably a new feature of C++03 compared to C++98. I wonder what he meant. Is value initialization part of C++98? Is it present in concept but not in name? Why was it added to the C++03 standard? I have a copy of the '03 standard but not the '98 standard. Here's the definition of default initialization and value initialization. To default-initialize an object of type T means: — if T is a non-POD class type

How to make a user-defined type initialize *exactly* like a built-in type?

别来无恙 提交于 2019-12-07 00:30:05
问题 I would like to make a type that wraps a numeric type (and provides additional functionality). Furthermore, I need the number and the wrapper to be both implicitly convertible to each other. So far I have: template<class T> struct Wrapper { T value; Wrapper() { } Wrapper(T const &value) : value(value) { } // ... operators defined here ... }; It's almost good, but it doesn't quite behave the same as a built-in type: #include <iostream> int main() { unsigned int x1, x2 = unsigned int(); Wrapper

VS2013 default initialization vs value initialization

一个人想着一个人 提交于 2019-12-06 21:32:02
问题 Consider the code below struct B { B() : member{}{}; int member[10]; }; int main() { B b; } VS2013 compiler gives the following warning: warning C4351: new behavior: elements of array 'B::member' will be default initialized 1> test.vcxproj -> C:\Users\asaxena2\documents\visual studio 2013\Projects\test\Debug\test.exe This is documented here With C++11, and applying the concept of 'default initialization', means that elements of B.member will not be initialized. But I believe that member{}