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

Douglas Gregor dgregor at apple.com
Thu Dec 15 10:19:12 PST 2011


On Dec 15, 2011, at 3:55 AM, Chandler Carruth wrote:

> 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 think the preprocessor use here is fine. It's ugly, but it's less ugly than the repetition is avoids.

Just one comment:

+  VariadicFunction() {}
+

This is not trivial, which is unfortunate. Please just leave out the default constructor so we'll get the implicitly-generated trivial default constructor. (The same goes for all of the VariadicFunction* class templates).

Also, I agree with David about the use of ArrayRef.

> I've also included Doug so that he can rant about adding hacks to LLVM/Clang to work around the absence of variadic templates. ;]

Someone can have fun adding a variadic-templates version for those of us who compile with C++11 support (so long as it's source compatible, naturally).

	- Doug




More information about the cfe-commits mailing list