How do I use args... with reference in C11?

Revision en1, by MiyanoShiho, 2021-03-11 13:50:01

Recently I wrote a fast read template and wanted to adapt it to multiple parameters.

The following is how I implemented it:

template <class T>
inline void read(T &x) {
  x = 0; int ch = getchar(); bool f = false;
  while (ch < '0' || ch > '9')
    ch = getchar(), f = (ch == '-') ? 1 : 0;
  while (ch >='0' && ch <='9')
    x = (x << 1) + (x << 3) + (ch ^ 48),
      ch = getchar();
  if (f) x = -x;
}

template <class T, class ...Args>
void read(T& x, Args... rest) {
  read(x), read(rest...);
}

However it completely crashed!

When I input 1 2 3 4, it just came out with 4 random int.

How should I improve that?

THX. XD

Tags #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English MiyanoShiho 2021-03-11 13:50:01 716 Initial revision (published)