11 ms·
The weirdest bug I’ve found in a compiler: MSVC 2017
- deleted 9y ago[deleted]
- acqq 9y agotemplate <typename U> using func_type = decltype(U{}.func()); I admit I'm not following all the new stuff that is added to C++. What are that two lines supposed to mean? I don't know what the U{}.func() and using x = decltype() constructs should mean or their names. The names of these and links highly appreciated. Thanks. Moreover, regarding the "bug": if "U" is actually special as an identifier (e.g. some predefined macro or something) it's a pity that the author hasn't investigated a little more. Even not knowing the meaning of the lines I've quoted, the symptoms look somewhat familiar to me with these "special behaviors" of identifiers.
- jchw 9y agoDidn't you read the rest of the article? They tested with arguments with different names. Further they showed that T was accessible from within the other template when it didn't have a template parameter T, making this even more bizarre. Also, just to be completely sure, I tested it myself. Using X: https://godbolt.org/g/B9YEgZ https://godbolt.org/g/B9YEgZ Showing the template parameter leaking with neither T nor U: https://godbolt.org/g/CKe2wG https://godbolt.org/g/CKe2wG
- dgellow 9y agoI know that’s a detail, but if I’m not mistaking the author is only one person, not sure why you wrote your comment in plural form (that personally slightly confused me).
- jchw 9y agoI am a purveyor of singular they, lacking any other good gender-neutral way to refer to a singular person in English.
- deleted 9y ago[deleted]
- jacquesm 9y agoBut in this case 'he' is a 'he' and that's clearly listed at the top of the article. So no need to be 'gender-neutral'.
- jchw 9y agoI know I don't have to, but I do anyways; personal choice. First time anyone's really even pointed it out.
- dgellow 9y agoI’m not a native English speaker myself, and I don’t live in a place with a majority of native speakers, so maybe that’s why. I got confused reading your comment as I thought that you were talking about something that I missed from the article (a link, or something else), thus I shared this feedback. Though I’m not sure why people downvoted my comment. I haven’t expressed anything against the use of neutral pronouns (though that’s unexpected when the author is clearly a man). And I clearly specified that was a detail, not something to give importance to.
- schrodinger 9y agoFWIW as a native English speaker I didn’t even notice that they (hah) used they. It sounds natural when you don’t know (or don’t care to emphasize) the gender of the singular person in question. Just trying to be helpful for the next time you notice that construct :)
- jchw 9y agoSheesh, I'm not exactly obsessed with internet points but I am kinda curious why I got so downvoted. I wasn't really trying to be condescending, but the article kinda does answer this concern (regarding U being reserved) in a couple ways.
- mdip 9y agoAt risk of being downvoted, myself, as this is off-topic, but since there is no 'meta' and you're clearly looking to understand what happened, I suspect the issue originates in the first statement made from the original comment, which was posed in the form of a question: Didn't you read the rest of the article? My rule of thumb is that if a question can have ", moron?"[0] appended to it and both the meaning and tone remain unchanged, then it's going to be taken as condescending. You'll probably find that people who are particularly sensitive to keeping Hacker News a "civil place" (among which I count myself) will read that sentence and skip the rest of the comment before hitting the downvote button. Had it been left off, the rest of your comment offered clarity to anyone who simply skimmed the article or otherwise wasn't understanding what was being presented and might have missed that point. Removing the sentence also doesn't affect the information being conveyed[1]. While I'll take you at your word that it wasn't meant as condescending, a variant of Hanlon's Razor[2] applies which I had shared with me as "Where there are gaps of understanding, people tend to jump to the worst possible conclusion." and this serves as a pretty good example. You assumed the reader didn't bother to read the article, when it was equally likely that they simply didn't understand what was being presented -- a fair estimate since this is C++ templates, which is not something everyone encounters in their day-to-day. And folks who are able to downvote likely did so because they assumed you were being condescending, when it's equally likely that you may have simply written the comment hastily or meant the question in a less-sarcastically sounding manner -- a fair estimate since verbal queues are missing from written text and most of us type near the speed that we think. [0] Or "dumba$$" or "?, God!" (followed by a gesture of hands being thrown down in disgust) [1] I once had a great manager who, at a yearly feedback session, told me rather bluntly that I am too verbose and worked with me on how to convey a message by learning what is unnecessary for conveying it. As you can tell if you glance through my comment history, the lesson was entirely lost on me. While I'm much better at eliminating things that add negativity to my intent (often by adding more to my statements), I'm pretty terrible at identifying when brevity has higher value than precision and completeness. Though, when it really matters, I can pull it together if I take enough time... and on and on and on. [2] https://en.wikipedia.org/wiki/Hanlon%27s_razor https://en.wikipedia.org/wiki/Hanlon%27s_razor
- tcbawo 9y agoHere is a link that describes value initialization: http://en.cppreference.com/w/cpp/language/value_initialization http://en.cppreference.com/w/cpp/language/value_initializati... and aggregate initialization: http://en.cppreference.com/w/cpp/language/aggregate_initialization http://en.cppreference.com/w/cpp/language/aggregate_initiali... Either of which could apply to 'U{}'. decltype is just returning the result type of the contained expression, which is the return type of the method 'func' called on a default or value initialized instance of type U. A lot of times people will use std::declval<U>() instead which returns a reference to U w/o needing to call a valid constructor (https://en.cppreference.com/w/cpp/utility/declval https://en.cppreference.com/w/cpp/utility/declval). The templated using statement is creating a template type 'func_type<U>' which is similar to a typedef.
- 8xde0wcNwpslOw 9y agoI can't guarantee I'm right, but AFAIK that's sort of a templated typedef, where the bound type is that of the return value of method "func" of class U. Short example: class Foo { int func(); }; // func_type<Foo> = int Also, for more info, see http://en.cppreference.com/w/cpp/language/type_alias http://en.cppreference.com/w/cpp/language/type_alias, https://en.cppreference.com/w/cpp/language/decltype https://en.cppreference.com/w/cpp/language/decltype.
- acqq 9y agoThanks, in type_alias document I see the example: template<class T> using Vec = vector<T, Alloc<T>>; // type-id is vector<T, Alloc<T>> Vec<int> v; // Vec<int> is the same as vector<int, Alloc<int>> That explains to me the using construct. And tcbawo's answer tries to address the U{}.func() However "either of which could apply to U{}" ... I'd expect that it should be possible to say which of these is in this example? I also can't see how that can (or should?) match "aggregate_initialization."
- pjmlp 9y agoThe expression is defining a type named func_type whose type value corresponds to the return type of the member function named func() in the given type.
- beached_whale 9y agoUsing allows for type aliases like typedef but with a more readable syntax. Decltype will take the type of the expression within. So the U{} constructs a value of type U and then the func() is called. So func type will either be complete when the struct has a func member function or not. The void_t is always void but only if the template arguments are complete. So when func exists it will be a true type else a false type. True and false type are integral constants that have a constant value member
- acqq 9y agoThank you, this is my favorite explanation.
- Matthias247 9y agoU is a class name, which is given as a template parameter. The braces are the new uniform initialization syntax, which basically means U{} constructs an object of type U. U{}.func() then calls the member function with the name "func" on the constructed object. decltype() returns the type of the contained expression. So I think it should return the return type of the member function. Then "using" assigns that type to an alias, as some typedef.
- multani 9y agoI tried remember reading an article on HN a couple of years ago about how MSVC got initial C++ support and added more and more features over time, despite having not being designed with C++ in mind originally. The result was a combination of clever hacks after clever hacks, with the consequence being the reputation MSVC got over time (I'm not saying this is one the consequence shown in the OP) Anyway, does anybody still have this article somewhere? I couldn't find it anymore :(
- jchw 9y agoMSVC has had C++ support for a long time, to be fair. GCC was probably just as bad at first, it just improved more steadily over time. Today, GCC and Clang both seem like very robust C++ compilers, and it doesn't feel like MSVC is terribly far behind (it does feels like it is behind, in my perception, just not far.)
- pjmlp 9y agoVisual C++ has the best C++ among commercial C++ compilers. http://en.cppreference.com/w/cpp/compiler_support http://en.cppreference.com/w/cpp/compiler_support And the table lacks most embedded compilers.
- fyi1183 9y agoThat feels like one of these misleading marketing statements. The real competition are GCC and clang. But yes, embedded compilers are usually a nightmare.
- pjmlp 9y agoThey are the competition when one only cares about *BSD, GNU/Linux, Windows systems.
- jchw 9y agoWell, and Android and iOS and many microcontrollers (well, often a shitty GCC fork in that case but still) and way more platforms (OpenBSD, Minix, HP-UX, Solaris, VxWorks, bare metal) and architectures (Alpha, ARM, AVR, x86, m68k, microblaze, MIPS, PDP-11, Risc-V, SPARC, VMS) than that, not including third party ports in SDKs. Yeah, I get that with embedded, it's a crapshoot. However it's probably not ridiculous to say that GCC ports to microcontrollers is not terribly uncommon, either. GCC and Clang certainly are a lot more useful than for just that tiny range you listed. The reality is they probably cover more platforms than any other individual compiler.
- TillE 9y agoI've crashed MSVC 15.7 repeatedly while writing templates - usually due to an error in my code, but sometimes on valid code. I really do appreciate that Microsoft has been developing their C++ compiler a lot faster than in the past, but this seems like a buggy release.
- jwilk 9y agoCopy of the article that works with JS disabled: https://gist.github.com/jwilk/b9f9142f204dfa634d3ec338af917232 https://gist.github.com/jwilk/b9f9142f204dfa634d3ec338af9172...
- TwoBit 9y agoI found years ago that (at least with older versions) VC++ implemented templates essentially as internally generated macros. All the non-standard behaviour they had could be explainined by considering how they would act if they were macros. This bug strikes me as being consistent with that. T is leaking across structs because it is internally #defined and seen by another struct. I think Microsoft has managed to prop up this massive template-as-macro hack all the way through VS 2017.
- ksherlock 9y agoI think as of 15.7, mentioned in TFA and released earlier this month, they do templates properly.
- TwoBit 9y agoI just found this statement from Microsoft: https://blogs.msdn.microsoft.com/vcblog/2017/09/11/two-phase-name-lookup-support-comes-to-msvc/ https://blogs.msdn.microsoft.com/vcblog/2017/09/11/two-phase... "MSVC previously recorded the body of the template as a stream of tokens and stored that stream away to be replayed at instantiation time. The behavior of MSVC’s template substitution from a recorded token stream somewhat resembled the behavior of macro substitution in that limited analysis was done of a template’s body."
- userbinator 9y agoAs someone who is admittedly not an expert in template metaprogramming by any means, thinking of templates as an extension of macros feels like the intuitive and simpler way compared to the more complex two-phase algorithm, so it's no surprise that MSVC did it too... then again, I prefer simpler languages (like C) and see the difficulty of implementing the standard as a sign that its complexity may be too high.
- TwoBit 9y agoThere's a reason why templates work like this. If they didn't work like this then template specializations that you write can break somebody else's code which is dependent on the template behaving a certain way.
- dingo_bat 9y agohttps://kek.gg/i/7_kBzW.png https://kek.gg/i/7_kBzW.png Fucking disgraceful.