jeudi 30 juin 2016

Reasons for using Variable Argument Lists versus vector (c++)

I am trying to understand Variable Argument Lists (varargs). I have previously used pointers and/or std::vector for parameters which can change in the amount of objects that need to be passed.

I would expect that Varargs (1,2) and Vectors (3,4) as part of function parameters both have their own special uses in C++ and are better at different things. However, I am having a difficult time distinguishing when std::vector is not the right choice. Could this please be explained?

Is this the type of scenario where varargs might be preferred?

union S //Using union from http://en.cppreference.com/w/cpp/language/union
{
    std::int32_t n;     // occupies 4 bytes
    std::uint16_t s[2]; // occupies 4 bytes
    std::uint8_t c;     // occupies 1 byte
};                      // the whole union occupies 4 bytes


int myFunction(std::vector<S> v)

Aucun commentaire:

Enregistrer un commentaire