Hacker News new | ask | show | jobs
by anonymous246 5699 days ago
You mean identical source code or identical assembly code?

I would expect that compiler strip away typdef'ing so unless Foo and Bar are really different, only one copy of the code should exist.

Yes, you can probably defeat this heuristic by doing a couple of pathalogical things, but I expect it work unless you're actively trying to defeat the system.

1 comments

identical assembly. No C++ compiler that I'm aware of does that.

e.g., std::vector<Foo * > and std::vector<Bar * > generate identical code for whatever you do, as they both only deal with the pointer, never dereference it in any way. Also, in code like

    template <class T> T get(T x[], int i) { return x[i]; }
on a 32 bit platform, get<int>() and get<char* > generate the same machine code, whereas on a 64 bit platform, get<long>() and get<void * >() generate the same machine code.

No C++ compiler that I'm aware of folds these cases into one; The C# generics do (not sure about non-generic code with identical final machine code). I would be surprised if idiomatic C++ code could be shortened by more than 50% by a compiler that did. However, if you write your code for column-major access, the binary can go down 90% if the compiler did that.

edit:formatting

The C++ committee answer to this is to use "partial template specialization". I'm hoping all the standard libraries I use such as Boost etc have already done this to avoid code bloat.

But I agree that it would be nice if the compiler did this automatically.