10 ms·
GCC Non-bugs
- webreac 12y ago"Most C++ compilers (G++ included) do not yet implement export". For me, non compliance with such an old spec is a bug.
- Sharlin 12y agoIt's more like export was a bug in C++98. It was removed without a deprecation period in C++11.
- webreac 12y agoI didn't know. My last professional development in C++ was in 2002. At that time http://www.comeaucomputing.com/ http://www.comeaucomputing.com/ was my reference.
- GFK_of_xmaspast 12y agoTurns out a lot has changed in the c++ world in the last 15 years. (trigraphs are going away too)
- X-Istence 12y agoThere is a single C++ compiler that supports export. Out of all the C++ compilers, only a single one supports it. Mainly because it is such a HUGE pain in the ass to write support for. See this paper as to why it was removed from the spec: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1426.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n142...
- andrewchambers 12y agoThose guys had serious dedication to essentially waste 3 years implementing it, just to say how useless it is.
- Retra 12y ago"John Spicer notes: “Any program written using export can be rearranged trivially into a program that doesn’t use export.”" This makes me wonder why they couldn't have implemented this rearrangement as export...
- andrewchambers 12y agoProbably trivially with some human judgment.
- userbinator 12y agoThat is a great counterpoint to this comment I saw recently: https://news.ycombinator.com/item?id=9126237 https://news.ycombinator.com/item?id=9126237
- markrages 12y agoIn my embedded development, I have trained myself to use unions instead of type-punned-pointer casts to access one data type as another. This document says this is a GCC-specific extension. Is this true? Or is it one of those things that's not standardized, but the compiler vendors all do it anyway?
- cremno 12y agoIt's legal C99. See DR258 / C99+TC3. But be careful, reading trap representations is UB. http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_283.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_283.htm http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
- effdee 12y agoType-punning via unions is allowed in both C99 [0] and C11. [0] see "ISO/IEC 9899:1999 Technical Corrigendum 3", http://www.open-std.org/Jtc1/sc22/wg14/www/docs/n1235.pdf http://www.open-std.org/Jtc1/sc22/wg14/www/docs/n1235.pdf
- oshepherd 12y agoType punning by pointer is explicitly UB per the standard. Type punning by union is implementation defined behavior. Everyone fortunately defines it to Do The Right Thing (TM). The truly standard supported way to type pun is by memcpy float f = ...; int x; memcpy(&x, &f, sizeof x);
- jakobegger 12y agoYes. Use memcpy if you need to do this! This is especially important when you're on a platform that requires aligned pointers. For example, the following code will crash on ARM char bytes[5] = {0}; float flt = *(float*)&(bytes[1]); Using memcpy works on any platform with 4 byte floats: char bytes[5] = {0}; float flt; memcpy(&flt, &(bytes[1]), 4);
- markrages 12y ago
- andrewchambers 12y agoSomething I found yesterday: static char * s = * &"hello"; This should not compile, clang refuses it, and it is not part of the C standard. GCC accepts it, but is it worth even submitting a bug report?
- plorkyeran 12y agoClang 3.5 compiles that for me without warnings even with -Weverything. String literals in C (but not C++) aren't const for legacy reasons despite not being modifiable.
- andrewchambers 12y agoSorry, I made a mistake. I think it is: static char s[] = * &"foo"; The c standard says char array initializers are either a string, or a string surrounded by optional '{}' .
- plorkyeran 12y ago-pedantic gives "warning: array initialized from parenthesized string constant" for that, so I'm guessing it's an unintended consequence of a nonstandard extension. Might be worth reporting since at the minimum the warning is wrong.
- shortstuffsushi 12y agoI know this is a problem with many programming languages, but why is there not a standard way to precisely represent decimal numbers? I understand the difficulty representing them in binary (well, kind of -- it's been a while since I read about it), but it seems like knowing this, a better solution would be found. Why is this accepted and standard behavior?
- krzysz00 12y agoIf by "decimal numbers" you mean the reals written in base 10, how do you propose we precisely represent the result of 1 / 3 . Or pi?
- shortstuffsushi 12y agoI guess I wasn't necessary referring to solving the problem of non-terminating numbers, but more things like 0.1 * 0.2 = 0.020000000000000004 in Javascript, or the very first bug listed on the linked page. Perhaps what you intend with that statement is that issues like that of 1/3 are a cause for these sorts of issues, but interestingly (at least in JS), 1/3 is terminating, and results in an "expected" value.
- dietrichepp 12y agoThe result of the computation 1/3 in JS results in a number which is terminating, but that number is not 1/3. Open up your console and type 1/3+1/3+1/3... 1, as expected. Then type 1-1/3-1/3-1/3... I get about 1e-16, which is not 0. The statement that "1/3 is terminating" is not true in binary, it is not true in decimal, and the only reason that you sometimes get the expected results is that sometimes the rounding errors will cancel out.
- valleyer 12y agoSo you're telling me we should use base 3.
- 12y ago
- zamalek 12y ago> Casting does not work as expected when optimization is turned on. > This is often caused by a violation of aliasing rules, which are part of the ISO C standard. These rules say that a program is invalid if you try to access a variable through a pointer of an incompatible type. Regarding this, is this what reinterpret_cast was at least partially designed for?
- pjmlp 12y agoKind of, casts in C++ were designed to be on your face, to call out that you are doing something unsafe. reinterpret_cast is meant to be really unsafe, to meant that in this type of casts the compiler will blindly do it, whereas the other *_cast there is still some type validation involved.
- zamalek 12y agoThanks for the clarification. So this doesn't signal to the compiler that it shouldn't optimize the aliased variables? Reason I ask and not test is that it seems that VC++ does not take advantage of this undefined behavior and the sample code always works 'as expected'.
- pjmlp 12y agoThis type of optimization is very compiler specific and can even change between versions of the same compiler. Actually this is the big difference between standard defined languages and those that rely on a single implementation. There is nothing to rely upon if it isn't defined in the standard as such.