News for hackers. Stuff that matters.
Re:Results of the Grand C++ Error Explosion Competition (2014) by WalterBright on Monday May 06, 2024 @08:35PM (#40281068)
In D, all semantic analysis of a template waits until instantiation time. This is because D is designed so that the syntax parsing does not need a symbol table.

In C++, I solved this problem by simply matching { } in the template body, and accumulating a list of tokens within the { }. Then, when instantiated, the template parameter values were known, and the template syntax could then be semantically analyzed. It was simple and effective.

But I was informed that C++ required the syntax parsing and semantics for non-dependent types without instantiation. I asked why, and the answer was "to check for errors without needing to instantiate it." I responded with "of what use is checking it if it is never used or tested?" And that was the end of that.

> The implementation of this is hugely complicated and expensive to maintain

I quietly revolted and refused to implement that disaster. AFAIK there was never a problem with deferring parsing/semantic until instantiation.

Re:Results of the Grand C++ Error Explosion Competition (2014) by Matheus28 on Monday May 06, 2024 @11:29PM (#40282073)
Isn't that what msvc does? I remember it's a little weird about when it actually checks for errors in templated code
Re:Results of the Grand C++ Error Explosion Competition (2014) by WalterBright on Tuesday May 07, 2024 @11:10PM (#40293979)
I have no idea what msvc does with this.
Re:Results of the Grand C++ Error Explosion Competition (2014) by tnh on Wednesday May 08, 2024 @05:36PM (#40302929)
Yes (at least approximately, I'm fuzzy on the details).

These days it supports both. (IIRC the default is legacy/nonstandard, you select the standard behavior with /fpermission-, and VS adds /fpermission- to newly generated projects)

https://devblogs.microsoft.com/cppblog/two-phase-name-lookup...

Re:Results of the Grand C++ Error Explosion Competition (2014) by tnh on Wednesday May 08, 2024 @05:27PM (#40302863)
Yeah, that trade-off makes a lot of sense. It's the logical conclusion of the "templates are textual" model. But C++ loves to have its cake and eat it regardless of the complexity, so it's non-conforming.

(I expect it's possible to construct cases where this difference is observable)

I think checking templates in isolation has value. We use statically typed languages in part to make more error classes locally-verifiable. But bolting that into a mostly-textual system is a mess.

(Checking templates in isolation is particularly valuable in IDEs, which tend to share logic with compiler frontends. IDEs only need that much power to do a passable job because the language is so complex, so I don't know which way this argument points)