I've tried my best to find an answer to my question, and while I did find some good resources, I still cannot wrap my head around this. First of all, I am not very familiar with the newish C++11 standard. Variadic templates and all that stuff, gets my head hurting real fast. Nor did I play much with the STL of C++, so even vectors are a bit of a left-field for me, and I like Python a lot, and most my experience is there. But recently surfing the web, I was curious about constructing my own MIDI player/creator and I found a nice resource. I will post it in the comments if context is required and asked for, but what I am having trouble understanding is this:
typedef unsigned char byte;
/* First define a custom wrapper over std::vector<byte>
* so we can quickly push_back multiple bytes with a single call.
*/
class MIDIvec: public std::vector<byte>
{
public:
// Methods for appending raw data into the vector:
template<typename... Args>
void AddBytes(byte data, Args...args)
{
push_back(data);
AddBytes(args...);
}
template<typename... Args>
void AddBytes(const char* s, Args...args)
{
insert(end(), s, s + std::strlen(s));
AddBytes(args...);
}
void AddBytes() { }
};
Starting from the very top, I have no clue what class MIDIvec: public std::vector does. I understand that-- actually, I am not even sure about that. I understand what the class is doing, but I do not understand the logic behind it.
First of all, it appears that wrapping vector MIDIvec allows MIDIvec to call vector member functions, is that correct? And second, I do not understand these templates- I do understand how they work, and I should really google this, and I did, but the explanation was really hard for me to understand in the sense of the code sample I provided.
Also, is Variadic Templates responsible for defining AddBytes three times? I am assuming based on the arguments, a different version of the function will be called that matches the arguments provided?
I feel like I am punching way above my weight, and that I am looking at the work of a master who took a lot of shortcuts in writing this code, even though he apparently did all the design and coding in one night, but I feel that my own understanding of programming will improve tremendously if I was to be helped to understand what was going on here.