My first dalliance with variadic templates

I just hacked together some code that I believe folks will find useful. It takes advantage of c++11’s variadic templates.

template<typename T>
bool inList(T a) { return false; }

template<typename T, typename... Args>
bool inList(T a, T b, Args... args)
if (a==b) return true;
return inList(a,args...);

You can use it to say something like:

int v = 23;
if (inList(v,12,55,23)) { do_something(); }

I wrote it in response to code I’ve had to write which repeatedly had to compare a variable against several alternatives.

Related Posts:

  • No Related Posts
This entry was posted in Programming and tagged , . Bookmark the permalink.

Comments are closed.