2

Once I saw a statement that "separately compiled C++ templates" is a standard feature that none of available C++ compilers support.

What are those "separately compiled templates" and why are they ignored?

Andriy Tylychko
  • 15,244
  • 5
  • 56
  • 103

2 Answers2

3

C++98 introduced the export keyword which allowed you to have the definition of a function template in another translation unit, with only its declaration needed to compile code that uses it. (See here if you are hazy on what's a definition vs. a declaration. Basically, you could have the function templates implementation in another translation unit.) That's just as it is with other functions.

However, only compilers using EDG's compiler front end ever supported it, and not all of them even did officially. In fact, the only compiler I know that officially supported it was Comeau C++. That's why the keyword, unfortunately, got removed from C++11.

I think it's safe to say that it is expected that a proper module system would cure C++ from many of its shortcomings that surround the whole compilation model, but, again unfortunately, a module system was not considered something that could be tackled in a reasonable amount of time for C++11. We will have to hope for the next version of the standard.

Community
  • 1
  • 1
sbi
  • 204,536
  • 44
  • 236
  • 426
  • 1
    Why "unfortunately"? Even the authors of the feature agree that it was poorly designed and specified, and there would be better ways to approach the problem. – Ben Voigt Oct 01 '11 at 21:39
  • 2
    @Ben: For all its shortcomings, it did remove the necessity to recompile your whole project because a helper template changed that's only used by a helper template only used by one template included everywhere. At least, Daveed Vandervorde (who implemented it) said that it did indeed provided this feature. At the time I was working for a company who had just spend a five-digit amount for [IncrediBuild](http://www.xoreax.com/visual_studio.htm), in order to get us productive again despite all the changing helper's helper templates in a several-MLoC project. `export` would've been cheaper. – sbi Oct 01 '11 at 21:48
  • Depracated? I thought it was flat-out removed. One of those things which really wouldn't break any existing code. – Kerrek SB Oct 01 '11 at 21:51
  • @sbi: broken link in your comment – Andriy Tylychko Oct 01 '11 at 21:52
  • In short, the humor that it didn't live up to its promises at all was mostly to this one article by Herb Sutter, which, according to Daveed, contained a lot of hyperbole and hearsay, and completely missed the fact I mentioned. To me, the removal was a bit rash, but understandable as a result of the fact that almost none of the vendors implemented it. – sbi Oct 01 '11 at 21:52
  • 1
    @sbi: Entirely right, Herb's article on `export` was not his finest. As I was still a voting ISO member at that point, I actually tested EDG's (Comeau's) `export` implementation at that point. It worked as specced, but probably not as people expected. E.g. you got nasty recompilations when you had `A` in a.cpp depend on `B` in b.cpp depend on `A` etc. – MSalters Oct 02 '11 at 01:10
0

Separately compiled templates is where you can bring in template definitions from another translation unit instead of having to define them in every TU (normally in the header).

Basically, they're ignored because they're virtually impossible to implement in terms of complexity and bring a number of unfortunate side effects.

Puppy
  • 138,897
  • 33
  • 232
  • 446
  • I think any working implementation would require extra permanent files to be written to the build directory, so this would really be a fairly intrusive feature. It's gone now, luckily. – Kerrek SB Oct 01 '11 at 21:51
  • Um, I seem to remember Borland C++ 3 having separate compilation of templates. That was about 20 years ago. – sbi Oct 01 '11 at 21:58