template <const size_t sizeofel,...> /* we'll fill the ellipses('...' with more params */
class base_vector
{
struct K {
char b[sizeofel];
};
/* use std::vector inside for K*/
std::vector<K> _karray;
};
template <typename T,...>
class my_vector
{
base_vector<sizeof(T),...> _array;
};
Question on template instantiation.
Suppose I have 2 classes of same size and alignment, and I would like to use it inside std::vector. Will std::vector get instantiated twice or will the compiler optimize for it so that both instances use some common code with specific type checking. For eg, the reserve or resize can have common code as the sizes of the classes are same.
In case the compiler does instantiate twice will it be worthwhile to write a generic template container class that can store elements based on their size. And wrap that class by a vector class that will implement similar functionality like std::vector.
What I mean is:
Will the above be efficient.
And if it will be then how should I use it inside a dynamic link library so that there are no multiple instantiation (for the class base_vector).
[EDIT]
Oh and I can destroy and construct the object using the new and delete operators that takes the address as input. So that wont be a problem too.
Any help will be appreciated.
Assuming the function isn't inlined (and for most of those one-off functions like size(), begin(), end(), they generally should be), some linkers will realize that they generated the same code for a function, and merge them.
However, things like resize have to call constructors, which can generate different code for different instantiations.
Is there a particular reason this matters for your scenario?
However, things like resize have to call constructors, which can generate different code for different instantiations.
Is there a particular reason this matters for your scenario?
Quote:Original post by RDragon1
Is there a particular reason this matters for your scenario?
No. Just wanted to know. I dont really think extensive std::vectors usage will result in code bloat. But there could be simple and better implementations too. So I was wondering.
I'm not aware of any compilers that will take two almost identical functions and combine them into one function with different code paths depending on what type or data it is dealing with. That kind of thing is pretty much in the realms of high-level optimisations handled by the programmer.
If they're exactly identical assembly code then yes it most likely will eliminate the duplicate (which can cause interesting behaviour during release build debugging), but if there is so much as a single difference in the generated assembly then it wont.
If they're exactly identical assembly code then yes it most likely will eliminate the duplicate (which can cause interesting behaviour during release build debugging), but if there is so much as a single difference in the generated assembly then it wont.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement