103 points · by ingve · 8y ago · 57 comments
I think Microsoft has managed to prop up this massive template-as-macro hack all the way through VS 2017.
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."
Anyway, does anybody still have this article somewhere? I couldn't find it anymore :(
As much as MS had the reputation of "Windows isn't done until Lotus won't run" back in the day. If you follow some of the MSDN blogs, you'll find the extraordinary hacks that MS has done over the years to keep the most obscure programs running.
People complain about how Apple will drop support for backwards compatibility but this a consequence of the MS culture of doing just the opposite.
Ironically Sutter was saying in C++ [sic] "we have a good backwards compatibility story". :)
http://en.cppreference.com/w/cpp/compiler_support
And the table lacks most embedded compilers.
But yes, embedded compilers are usually a nightmare.
https://blogs.msdn.microsoft.com/vcblog/2017/09/11/two-phase...
https://blogs.msdn.microsoft.com/vcblog/2015/09/25/rejuvenat...
In most places I worked for it actually seemed to have the reputation of a fairly good compiler toolset, unfortunately with just a bit of non-standardness sprinkled on top of it, a bug here and there, and perhaps not the fastest out there. (Which describes pretty much every other compiler out there as well: it's not like there's a perfect one with no quirks whatsoever). But it does the job, the optimization is ok as well, and it usually comes integrated in VS which is top of the line compared to other IDEs.
template <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.
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).
The templated using statement is creating a template type 'func_type<U>' which is similar to a typedef.
class Foo {
int func();
};
// func_type<Foo> = int
Also, for more info, see http://en.cppreference.com/w/cpp/language/type_alias, https://en.cppreference.com/w/cpp/language/decltype. 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."
Also, just to be completely sure, I tested it myself.
Using X: https://godbolt.org/g/B9YEgZ
Showing the template parameter leaking with neither T nor U: https://godbolt.org/g/CKe2wG
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.
As you say, "kinda." Obviously, the way I understood it, it kinda doesn't. It does make some simple tests, but fails short from concluding (or clearly stating) anything specific in the text of the article. The way I read the article, it still appears to me that there are some specific names active in the scopes used, the way they are used, and that author's code hints at them.
Please don't insinuate that someone hasn't read an article.
[...]
Please don't comment about the voting on comments.
https://gist.github.com/jwilk/b9f9142f204dfa634d3ec338af9172...