value-initialization

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

江枫思渺然 提交于 2019-12-05 04:46:31
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<unsigned int> y1, y2 = Wrapper<unsigned int>(); std::cerr << x1 << std::endl; // uninitialized, as

VS2013 default initialization vs value initialization

ぃ、小莉子 提交于 2019-12-05 02:13:34
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{} should perform value initialization and not default initialization. Is the VS2013 compiler broken? $8.5/6

The behavior of value-initializing an enum

笑着哭i 提交于 2019-12-02 18:51:30
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 T . However, an integral constant zero is not implicitly convertible to an enum. Ultimately, an enum

Does value initialization work for atomic objects?

混江龙づ霸主 提交于 2019-11-30 03:31:12
问题 By work here, I take to mean that std::atomic<T> a{} effectively zero initializes a . I have always been thinking so and have been practically using it until this. Before explaining my understanding of this, I want to show that, at the very least, gcc and clang are doing it in practice. #include <cstring> #include <atomic> #include <iostream> int main() { using atomic = std::atomic<int>; auto p = (atomic*)operator new(sizeof(atomic)); std::memset(p, -1, sizeof(atomic)); new(p) atomic{}; std:

What does 'value initializing' something mean? [duplicate]

大城市里の小女人 提交于 2019-11-28 18:23:06
Possible Duplicate: What do the following phrases mean in C++: zero-, default- and value-initialization? If I have a class for example: class Info { int x; int y; }; which I used to created an object, Info *p = new Info(); Does the brackets beside Info mean i'm value initializing it? How does it different from this, Info *p = new Info; ? I know there is a question which differentiate between different meanings in new and old C++ language but I want to know the semantic difference between default and value initialization e.g. Does value initialization means initializing something to zero? A

Explicit Type Conversion and Multiple Simple Type Specifiers

梦想与她 提交于 2019-11-27 14:29:29
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++ standard (C++03 5.2.3/2, expr.type.conv): The expression T() , where T is a simple-type-specifier (7.1.5.2)

How do I mock an autowired @Value field in Spring with Mockito?

我与影子孤独终老i 提交于 2019-11-27 06:15:32
I'm using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value("#{myProps['default.url']}") private String defaultUrl; @Value("#{myProps['default.password']}") private String defaultrPassword; // ... From my JUnit test, which I currently have set up like so: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:test-context.xml" }) public class MyTest { I would like to mock a value for my "defaultUrl" field. Note that I don't want to mock values for the other fields — I'd like to keep those as they are, only the "defaultUrl" field. Also note that I

What does 'value initializing' something mean? [duplicate]

China☆狼群 提交于 2019-11-27 05:29:10
问题 Possible Duplicate: What do the following phrases mean in C++: zero-, default- and value-initialization? If I have a class for example: class Info { int x; int y; }; which I used to created an object, Info *p = new Info(); Does the brackets beside Info mean i'm value initializing it? How does it different from this, Info *p = new Info; ? I know there is a question which differentiate between different meanings in new and old C++ language but I want to know the semantic difference between

Value initialization and Non POD types

一曲冷凌霜 提交于 2019-11-26 17:36:34
An hour ago I posted an answer here which according to me was correct. However my answer was downvoted by Martin B . He said You're just lucky and are getting zeros because the memory that i was placed in happened to be zero-initialized. This is not guaranteed by the standard. However after reading Michael Burr's answer here and trying the following sample code 1) #include <cassert> struct B { ~B(); int m; }; int main() { B * b = new B(); assert(b->m == 0); } I got a debug error on MSVC++ 2010. I got a similar error when I tried the following code [My answer here ] on MSVC++2010 2) #include

How do I mock an autowired @Value field in Spring with Mockito?

…衆ロ難τιáo~ 提交于 2019-11-26 10:13:17
问题 I\'m using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value(\"#{myProps[\'default.url\']}\") private String defaultUrl; @Value(\"#{myProps[\'default.password\']}\") private String defaultrPassword; // ... From my JUnit test, which I currently have set up like so: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ \"classpath:test-context.xml\" }) public class MyTest { I would like to mock a value for my \"defaultUrl\" field. Note that I don\'t want to