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
bool inList(T a) { return false; }

template
bool inList(T a, T b, Args... args)
{
if (a==b) return true;
else
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.