c++ - Is it possible to fix an argument to a variadic template function? -


i want put variable number of arguments buffer, , i've overloaded << operator insert values given buffer:

template<typename t, typename... args> void pack(t first, args... args, std::vector<uint8_t> &buffer) {     pack(args ..., buffer);     buffer << first; }   void pack(std::vector<uint8_t> &buffer) { return; } 

since variadic template functions rely on recursion, idea have base case returns buffer , recursive case inserts first argument buffer (after recursive call).

however, if run code like:

std::vector<uint8_t> buffer; pack((uint8_t)0xff, (uint32_t)0x0a, buffer); hex_dump(buffer); 

i error message saying:

in file included main.cpp:2:0: ./misc.hpp:20:6: note: candidate: template<class t, class ... args> void pack(t, args ..., std::vector<unsigned char>&)  void pack(t first, args... args, std::vector<uint8_t> &buffer);       ^~~~ ./misc.hpp:20:6: note:   template argument deduction/substitution failed: main.cpp:35:47: note:   candidate expects 2 arguments, 3 provided      pack((uint8_t)0xff, (uint32_t)0x0a, buffer); 

how can pass buffer in recursive case, can append value partially filled buffer?

simpler have buffer @ first argument:

void pack(std::vector<uint8_t> &) {}  template<typename t, typename... args> void pack(std::vector<uint8_t> &buffer, t first, args... args) {     buffer << first;     pack(buffer, args ...); } 

and in c++17, fold expression, may directly write:

template<typename t, typename... args> void pack(std::vector<uint8_t> &buffer, args... args) {     (buffer << args << ...); } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -