[llvm-dev] Variadic templates for llvm::opt::ArgList/llvm::opt::option::matches

<Alexander G. Riccio> via llvm-dev llvm-dev at lists.llvm.org
Thu Jan 28 00:24:59 PST 2016


I'm curious: did anybody ever consider using variadic templates for the
highly redundant overloads in ArgList.cpp? It's a very textbook use-case
<http://eli.thegreenplace.net/2014/variadic-templates-in-c/>.

"Templates are confusing to many programmers" is a perfectly valid answer.

Code like seven of these:

Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
                         OptSpecifier Id2, OptSpecifier Id3,
                         OptSpecifier Id4, OptSpecifier Id5,
                         OptSpecifier Id6, OptSpecifier Id7) const;

...could be replaced by code that looks something like this:

//In llvm/Option/Option.h
template<typename Opt>
bool Option::matches(T Opt0) const {
  return Option::matches<OptSpecifier>(Opt0);
}

template<typename T, typename... Opts>
bool Option::matches(T Opt0, Opts... opts) const {
  return Option::matches<OptSpecifier>(Opt0) || matches(opts...);
}

//In llvm/Option/ArgList.cpp
template<typename... Ids>
Arg *ArgList::getLastArg(Ids... ids) const {
[...loop here...]
  if((*it)->getOption().matches(ids...))
  Res = *it;
  Res->claim();
[etc...]


Sincerely,
Alexander Riccio
--
"Change the world or go home."
about.me/ariccio

<http://about.me/ariccio>
If left to my own devices, I will build more.
⁂
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160128/c0bd717e/attachment.html>


More information about the llvm-dev mailing list