I'm not fully familiar with templating but I need this to be inheritable and std::vector just spews warnings so I decided to just make my own mimicking it.
I'm getting this error: Error 4 error C2955: 'xsVector' : use of classtemplate requires template argument list c:\me\prjs\cpp\hackerex\hackerex2\hexdb_hack.h 101 1 hexDB
from this header file code:
Peter87:
xsDLL is defined from "hexDLL_zxStuff.h", it's for when I want it exported from a DLL which in this case is true. If there is something wrong with the way I did it then I would like to know now if possible. I'll add that headers code below. Edit: Just tried without and there was countless errors because I was trying to export code that otherwise
code777:
I want it inherited because it uses less bytes and is less of a pain to code for, as for specific type what do you think class xsDLL Hacks : public xsVector< Hack > is? I only omitted the rest of the class and the info about the Hack class, I thought the line was enough to show how I was using it but anyway thank you for clearing it up on that side of things.
Would you be so kind as to explain why it's not a good idea to inherit from a templated class?
Well, template is dealing with unknown data. This is hard enough (and error prone) on the spot. Inheritance adds another level of abstraction. It's like throwing knives now blindfolded...
@OP: show the code where the error occure (maybe you forgot a #include?)
There's a whole bunch of them so I figured it had something to do with how I created it or how I'm inheriting it, since it's all done the same way with the only thing being used is classes and numbers I figured only one was necessary.
For now while I wait I going to convert all my files to ANSI to stop VC from making incorrect complaints
Is there a compelling reason not to use std::vector<>?
> Well, template is dealing with unknown data. This is hard enough
> (and error prone) on the spot. Inheritance adds another level of abstraction.
> It's like throwing knives now blindfolded...
And what would CRTP be? Throwing hand grenades, blindfolded, in a dark room, while hanging on to the ceiling with one's teeth?
JLBorges:
The reason I need my own class is because I need it to be inherited into classes that are exported from DLLs, VC throws out a lot of complaints when I try this with std::vector<> so designing one from scratch that is intended to support exporting was the best solution I could come up with.
I also decided to use the same functions to avoid a need to document as there would already be usable documentation available.
> The reason I need my own class is because I need it to be inherited into classes that are
> exported from DLLs, VC throws out a lot of complaints when I try this with std::vector<>
Writing your own vector class only side steps one problem.
What would happen if a std::bad_alloc is thrown in the dll?
To ensure that you do not violate ODR, what you have to do is simple; ensure that every component in the program (the executable and the dlls) use the same shared instance of the standard library. Build all of them to link with the the DLL version of the run-time library - either /MDd (debug) or /MD (release) . http://msdn.microsoft.com/en-us/library/2kzt1wy3(v=vs.100).aspx
For instance, this will work seamlessly if we build both with /MDd:
While I thank you for your reply, std::bad_alloc won't be an issue at the moment however I will take that into consideration. For reference I am also using wxWidgets so if I can avoid using the std namespace then I will (Which is why I used a different name altogether).
If you have a suggestion for the root class that can deal with such instances then I would be happy to hear it.
The way I understand it is that when you use a template your compiler sets up what my book called an "instance" of that template for each time you call that template using a different variable-type combination and that instance is held in memory until program end. So if you have a templated class that has several other classes that are derived from it then the compiler is going to make an instance for each variable type in each object and in each derived object type, so it can quickly use up a tonne of memory for even short codes. That's how I understand it at least.
#include <vector>
template < typename T > T foo( const T& v )
{
std::vector<T> a ;
for( int i = 0 ; i < 1000 ; ++i ) a.push_back(v) ;
return a.front() ;
}
int main()
{
int i = 7 ;
double d = 0 ;
short s = 8 ;
float f = 3.4 ;
return foo<int>(7) /* + foo<double>(d) + foo<short>(s) + foo<float>(f) */ ;
}
This has one instantiation of std::vector<>.
Compile and build it with optimizations enabled ('Release' build in VS). Note the size of the executable.
Uncomment the commented part of line 17. return foo<int>(7) + foo<double>(d) + foo<short>(s) + foo<float>(f) ;
Now there are four instantiations of std::vector<>.
Compile, build, and check executable size as before.
That would give you a rough idea about the memory cost involved in the instantiation of std::vector<T>.
On your implementation, of course.
Thanks, I wasn't trying to reinvent the "wheel", just trying to use something guaranteed to not cause compile complaints, exceptions and bad_alloc I can deal with later.
I didn't realise about the memory but I've already thought of a solution, I will make a macro using the same/similar code to xsVector and it will fill part of the class inheriting it itself e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifdef xsDEBUG
class Codes : public xsVector< Code, 0x100 >
{
public:
int selection;
}
#else
class Codes
{
xsVECTOR( Codes, Code, 0x100 );
public:
int selection;
}
#endif
In this way I can still keep the xsVector class for testing new code/functions while being able to deal with memory for releases.