3 ms·
What has to change in C++ templates for this to work? It seems particularly tricky to define a template in a module and then instantiate it or specialize it so
by fooker 1y ago
What has to change in C++ templates for this to work?
It seems particularly tricky to define a template in a module and then instantiate it or specialize it somewhere else.
- WalterBright 1y agoIn order to make things work smoothly, the module has to have its own namespace, and a namespace that is closed. D also has an `alias` feature, where you can do things like: alias Q = abc.T; where from then on, `abc.T` can be referred to simply as `Q`. This also eliminates a large chunk of purpose behind the preprocessor.
- fooker 1y agoNeat, closed namespaces sound great! C++ has adapted the ‘using’ keyword now to seem fairly similar to alias, but can’t completely subsume macros unfortunately.
- WalterBright 1y agoClosed namespaces means the module can be reliably imported anywhere without changing its semantic meaning.
- Gibbon1 1y agoI've really wanted that in C for a long time. It seems like a very trivial thing as well.
- WalterBright 1y agoYeah, it's actually easy to implement, too. It replaces the preprocesser: #define Q abc.T with hygiene. Once you get used to it, it has all kinds of nice uses.
- StilesCrisis 1y agoC++ code generally uses `using` for typedefs. Macro-based typedefs are exceptionally rare and frowned upon.
- WanderPanda 1y agoI think modularization of templates is really hard. Best thing I can think of is a cache e.g. for signatures. But then again this is basically what the mangling already does anyways in my understanding.
- fooker 1y agoNot just signatures, you need the whole AST more or less. This seems incredibly wasteful, but of course still marginal better than just #including code which is the alternative.
- jjmarr 1y agoIncluding or importing a templated class/function should not require bringing in the definition. That's why #includes and imports are so expensive, as we have to parse the entire definition to determine if template instantiations will work. For normal functions or classes, we have forward declarations. Something similar needs to exist for templates.
- WalterBright 1y agoAnd it doesn't in D. We call such modules ".di files", which consist only of declarations. D does not require names in global scope to be declared lexically before they are used. C++ only does this for class/struct scopes. For example: int bar() { foo(); } int foo() { bar(); } compiles and runs (and runs, and runs, and runs!!!).
- jjmarr 1y ago> D does not require names in global scope to be declared lexically before they are used. C++ only does this for class/struct scopes. But how do you handle a template substitution failure? In C++: template<typename T> auto bar(T x, T y) { return x + y;} The compiler has no idea whether bar(1, 2); will compile unless it parses the full definition. I don't understand how the compiler can avoid parsing the full definition. The expensive bit in my experience isn't parsing the declaration, it's parsing the definition. Typically redundantly over thousands of source files for identical types.