[cfe-commits] PATCH: Add VariadicFunction class template to LLVM's ADT

Chandler Carruth chandlerc at google.com
Thu Dec 15 03:55:57 PST 2011


Hello folks,

This patch adds a generic collection of class templates to the LLVM ADT for
building variadic-like functions in C++98. The idea, from the comments, is:

/// Suppose we need a variadic function like this:
///
///   ResultT Foo(const ArgT &A_0, const ArgT &A_1, ..., const ArgT &A_N);
///
/// Instead of many overloads of Foo(), we only need to define a helper
/// function that takes an array of arguments:
///
///   ResultT FooImpl(const ArgT *const Args[], int Count) {
///     // 'Count' is the number of values in the array; 'Args[i]' is a
pointer
///     // to the i-th argument passed to Foo().  Therefore, write
'*Args[i]'
///     // to access the i-th argument.
///     ...
///   }
///
/// and then define Foo() like this:
///
///   const VariadicFunction<ResultT, ArgT, FooImpl> Foo;

We used this pattern extensively to build up the AST matcher library on top
of Clang (and so I've included cfe-commits in case anyone there is
interested). In preparation for mailing patches for those matchers, I'm
sending a patch that just adds the generic utility to the ADT library as
that seems like a natural home for it.

Previous versions of this bit of helper code used a fully expanded source
file that would be hard to maintain. I was worried about using the
preprocessor to expand these constructs, but it turned out *much* easier
than I had feared, and I think the result is much easier to read and
maintain. That said, if anyone dislikes the preprocessor hammering, I'm
happy to switch to a more manually expanded form.

I've also included Doug so that he can rant about adding hacks to
LLVM/Clang to work around the absence of variadic templates. ;]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111215/1295a369/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: variadic-function.patch
Type: application/octet-stream
Size: 11662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111215/1295a369/attachment.obj>


More information about the cfe-commits mailing list