4 ms·
Including 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 t
by jjmarr 1y ago
Including 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.