Re: [OT] Re: C++ *for Git*
From: David Kastrup <hidden>
Date: 2016-06-15 22:43:36
"Marco Costalba" [off-list ref] writes:
On 9/23/07, David Kastrup [off-list ref] wrote:quoted
"Marco Costalba" [off-list ref] writes:quoted
And BTW template <typename T> is the thing in C++ that more remembers me of opaque pointers and their use in C, the difference is that the first is fully type checked.Not really. The difference is that the first generates new (and optimized) code for every type which is something you can only do using macros in C. Class programming is similar to opaque pointers (in particular concerning the generated code) but templates are really more like macros, as their instantiation generates specialized code, not at all like the handling of opaque pointers.Probably if I had written like this was more clear: template <typename T> int some_function(T* p);
Huh? How is this supposed to support your point? There is nothing like an opaque pointer involved here. The point of opaque pointers is that they can stand for a variety of types, whereas each template instantiation can only substitute a single type.
And regarding 'new' code for each type I would like to remember that template instantations of different types can be removed by compiler/linker when the instantations are the same (i.e. produce the same binary instuctions), this could happen for function templates that handle pointers, as example.
Hardly. The type constraints/virtual function tables of any called function depending on T will be different. And if indeed nothing depends on T at all inside of the template, it is pointless not to declare it as void *p in the first place: the type of *p will never be used then. -- David Kastrup, Kriemhildstr. 15, 44793 Bochum