« first day (1078 days earlier)      last day (1778 days later) » 

nwp
10:31 AM
By copying it. In practice for `std::size_t` the compiler should optimize out the copy.
The better solution would be to replace the original C array with `std::array` to avoid the issue.
 
 
2 hours later…
nwp
12:31 PM
Why does std::ostringstream ss{"First: "}; ss << "Second"; std::cout << ss.str(); print only Second and not First: Second? The documentation states that the parameter is used as the initial content and operator << should only add to it, not overwrite it.
 
 
2 hours later…
jrh
2:38 PM
struct TestStructB
{
    int m_a;
    TestStructB() = default;
};

int main()
{
    TestStructB z; // indeterminate value for m_a
    TestStructB a = TestStructB(); // m_a initialized to 0
    return 0;
}
Am I correct in saying that m_a is indeterminate for z because the default constructor isn't called, or is it more true to say that it is the intended behavior of the default constructor that it only initializes m_a to 0 if it is called explicitly as it is for a?
 
nwp
TestStructB a = TestStructB(); // m_a initialized to 0 is not actually true.
Both versions should do the exact same thing, which is calling the default constructor which doesn't initialize m_a.
 
jrh
in MSVC a has a m_a of 0 and z has an uninitialized m_a; I didn't think that this was a VS "feature", let me try it in g++
 
nwp
It's undefined behavior. Anything can happen. Especially when you run under the debugger it tends to zero out the stack before starting the program.
 
jrh
same behavior on gcc, FWIW
 
nwp
What you can do is using aggregate initialization like with C arrays. TestStructB z{}; will value-initialize m_a to 0.
 
jrh
2:45 PM
I'm actually a bit relieved that it's UB because I couldn't find any authoritative references as to what TestStructB() would be, or how it would differ from TestStructB z
 
nwp
The better solution is probably to just do struct TestStructB{ int m_a = 0; }; so screwing up becomes more difficult.
 
jrh
yeah, that would be what I would do for a POD
 
nwp
Also works great on non-PODs.
 
jrh
I'm just making sure I know what is and isn't a valid initialization
I was pretty surprised to see TestStructB() do what it did
thanks
@nwp well, hold on, there's something funny about this
    TestStructB a = TestStructB(); // m_a initialized to 0
0120460C  xor         eax,eax
0120460E  mov         dword ptr [a],eax
01204611  mov         dword ptr [ebp-2Ch],eax
01204614  mov         dword ptr [ebp-28h],eax
01204617  mov         dword ptr [ebp-24h],eax
If I extend TestStructB with an additional 3 fields, the compiler is definitely emitting code to set all of its fields to 0
(new struct)
struct TestStructB
{
    int m_a, m_b, m_c, m_d;
    TestStructB() = default;
};
Not saying it isn't UB but this is certainly not just zeros happening to be in the stack
 
nwp
Hmm, I don't know. Clang agrees with you.
 
jrh
2:55 PM
The spec either doesn't explain what TestStructB() in this case is, or it explained it in a way that didn't differentiate it from an uh... non-default parameterless constructor?
it doesn't help that some stuff on the web says that a default constructor is any constructor that doesn't have parameters...
I'm finding it hard to search for this
do you think it's time for me to post this as a language lawyers question on the site?
 
nwp
The default constructor and the TestStructB() = default; is a red hering. It has nothing to do with anything.
 
jrh
yeah, I can comment it out with no change to the behavior
 
nwp
> when a nameless temporary object is created with the initializer consisting of an empty pair of parentheses
 
jrh
ah... nameless temporary
 
nwp
Found through asking the duck for "c++ temporary value-initialized".
 
jrh
3:00 PM
thanks again!
 
nwp
Would probably make for a good fun lame pop quiz.
 
jrh
Another C++ cute technique that I should never, ever use again
 
nwp
3:26 PM
It does kinda make sense. If it's a temporary you can't initialize it later.
 
 
1 hour later…
4:50 PM
@nwp Kind of unexpected, yeah. The ostringstream's constructor basically just forwards to the stringbuf's constructor, which says it works as if by str(s). Then, "For non-appending output streams, pptr() == pbase(), so that subsequent output will overwrite the characters copied from s".
Note that ostringstream::str has the same behavior; look at the example.
If you want to append, stringbuf::str states: "For append streams (mode & ios_base::ate == true), pptr() == pbase() + s.size(), so that subsequent output will be appended to the last character copied from s"
So std::ostringstream ss{"First: ", std::ios_base::ate}; does what you want.
The intuition is right if you view the string passed to the ostringstream as a "file".
 
 
3 hours later…
7:26 PM
Hello
Why again is bit shifting in C / C++ platform independent?
 
@Nils About the only thing that's platform dependent is right shifting a negative value. That's platform dependent because there are two possibilities: logical right shift,which zero-fills the upper bits, and arithmetic right shift which (at least on 2's complement) fills the upper bits with a replica of the current upper bit. Most 2's complement machines have instructions for both.
If you do a logical right shift on a negative number, you won't get anything similar to division by 2. For example on a 4-bit machine, -5 will be represented as 1011. If you shift that right and fill the top bit with a zero, you get 0101, which is +5. If you fill the top bit with 1, you get 1101, which is -2.
 
8:08 PM
Humm ok but what about little or big endian?
 
@Nils Makes no difference. With non-negative numbers, a right shift always divides by 2 and a left shift always multiplies by 2, regardless of endianess.
 
ok
Can I choose between logical and arithmetic shift in C++?
 
@Nils If you use unsigned, you always get logical shift. About the only way to get an arithmetic right shift is to divide by 2.
 
Isn't that "impl defined"?
In Java there is >>> for a logical shift?
 
@Nils No--right shift of an unsigned is always well defined. It's only right shift on negative numbers that's implementation specific.
@Nils Seems right, since they don't have any unsigned types to use.
 
8:17 PM
Ah and with C++ you are on the safe side if you use unsigned types for bit shifting.
Thanks for such an elaborate answer @JerryCoffin!
 
@Nils Surely.
 
 
2 hours later…
jrh
10:13 PM
One thing to remember, endianness and how bits are represented at the hardware level (as in which bit is the highest significance and which bit is the lowest significance) is abstracted away by the compiler and probably the hardware itself too. That goes for both bit endianess and byte swap; you don't have to worry about it.
@Nils The only time you have to worry about stuff like that is when you're reading or writing binary data, e.g,. sending data over a socket, or reading binary data from a different computer than the one that wrote it; if you're saving binary files, you definitely do need to standardize on endianness
 
10:31 PM
@jrh Well, you'd certainly want to standardize on endianess if you were sane anyway. Or, you could do like (for example) the utterly insane designers of the ESRI shapefile format, which consists of 7 items in big endian, followed by 10 items in little endian. esri.com/library/whitepapers/pdfs/shapefile.pdf
 
jrh
"it's not a portability bug, it's a file format standard"
 
@jrh "With friends standards like these, who needs enemies?"
 
jrh
@Nils oh, one other case where bit ordering matters is if you're interfacing with ports / memory mapped I/O on, e.g., embedded code, driver / OS internals, or DOS
 

« first day (1078 days earlier)      last day (1778 days later) »