DOC PREVIEW
vararg_templates_n1483

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

IntroductionMotivationVariable-length template parameter listsTypesafe Variable-length Function Parameter ListsArgument forwardingSyntax and SemanticsVariable-length template parameter listsTypesafe variable-length function parameter listsForwarding function argumentsArgument type deductionPartial ordering of vararg class template partial specializationsOverloadingTuple cons operationFunction traitsExamplesBuilding BlocksMember pointer adaptor implementationTuple implementationFunction implementationBind implementationTypesafe Variable-length Function and Template Argument ListsDouglas Gregor Gary Powell Jaakko J¨arviDocument number: N1483=03-0066Date: 25 April 2003Project: Programming Language C++, Evolution Working GroupReply-to: Douglas Gregor <[email protected]>1 IntroductionThis proposal addresses three problems under a unified framework:• The inability to instantiate class and function templates with an arbitrarily-long list of template pa-rameters.• The inability to pass an arbitrary number of arguments to a function in a type-safe manner.• The argument forwarding problem.The proposed resolution is to introduce a syntax and semantics for variable-length template argumentlists (usable with function templates via explicit template argument specification and with class templates)along with a method of argument building using the same mechanism to pass an arbitrary number of functioncall arguments to a function in a typesafe manner.2 Motivation2.1 Variable-length template parameter listsVariable-length template parameter lists (varargs) allow a class or function template to accept some number(possibly zero) of template arguments beyond the number of template parameters specified. This behaviorcan be simulated in C++ via a long list of defaulted template parameters, e.g., a typelist wrapper mayappear as:struct unused;template<typename T1 = unused, typename T2 = unused,typename T3 = unused, typename T4 = unused,/* up to */ typename TN = unused> class list;This technique is used by the Boost Tuples library [10], for the specification of class template std::tuple<>in the library TR [11], and in the Boost metaprogramming library [9]. Unfortunately, this method leadsto very long type names in error messages (compilers tend to print the defaulted arguments) and very longmangled names. It is also not scalable to additional arguments without resorting to preprocessor magic [13].In all of these libraries (and presumably many more), an implementation based on template varargs wouldbe shorter and would not suffer the limitations of the aforementioned implementation. The declaration ofthe list<> template above may be:template<...> class list;1Doc. no: N1483=03-0066 2This language feature is primarily designed for library developers, and is drawn from experience in librarydevelopment where these techniques have been simulated in one way or another [9, 12, 10, 5, 1]. Noviceusers aren’t likely to use this feature, as generically manipulating heterogeneous type container (tuples, inthis case) requires some degree of metaprogramming.2.2 Typesafe Variable-length Function Parameter ListsVariable-length function parameter lists allow more arguments to be passed to a function than are declaredby the function. This feature is rarely used in C++ code (except for compatibility with C libraries), becausepassing non-POD types through ... invokes undefined behavior. However, a typesafe form of such a featurewould be useful in many contexts, e.g., for implementing a typesafe C++ printf that works for non-PODtypes. The lack of such a facility has resulted in odd syntax for formatting in the Boost.Format library [14]:format("writing %1%, x=%2%: %3%-th try") % "toto" % 40.23 % 502.3 Argument forwardingThe forwarding problem, as described in [4], is:For a given expression E(a1, a2, ..., an) that depends on the (generic) parameters a1, a2, ..., an, it isnot possible to write a function (object) f such that f (a1, a2, ..., an) is equivalent to E(a1, a2, ..., an).The primary issue with the argument forwarding problem is to deduce the types of a1, a2, ..., ansuchthat they can be forwarded to the underlying function E without changing the semantics of the expression.The common implementation idiom is to accept a (non-const) reference for each parameter, and to provideoverloads for some number of arguments to forward, e.g.:template<typename E, typename T1>void f(E e, T1& a1) { e(a1); }template<typename E, typename T1, typename T2>void f(E e, T1& a1, T2& a2) { e(a1, a2); }template<typename E, typename T1, typename T2, typename T3>void f(E e, T1& a1, T2& a2, T3& a3) { e(a1, a2, a3); }// repeat up to N argumentsHowever, this idiom fails to accept literal values and also requires repetition for an arbitrary numberof arguments (often done via preprocessor metaprogramming in the Boost libraries [13]). This proposalsolves both issues: arguments are passed via the “Const + Non-const reference” option #3 enumeratedin [4], which results in “perfect” forwarding of arguments, but does not require 2Noverloads to support Narguments: instead, a single overload suffices for any number of arguments. The solution matches all threecriteria (C1-C3) of the argument forwarding paper [4] by bundling the arguments into a single argument andproviding an unbundling method to pass the arguments on to another function.3 Syntax and Semantics3.1 Variable-length template parameter listsThe template parameter list to a function or class template can be declared to accept an arbitrary numberof extra template arguments by terminating it with “...” optionally followed by an identifier that will holda tuple of the extra arguments. Any number of template parameters may precede the “...”. Thus we candefine, for instance, a class Foo accepting a template type parameter followed by an arbitrary number oftemplate arguments as:Doc. no: N1483=03-0066 3template<typename T, ... Elements> class Foo;Here, T is the name of the template type parameter and Args is the name of a std::tuple<> [11]containing the (possibly empty) set of template arguments given to Foo. Given the instantiation Foo<int,float, double, std::string>, Elements would be std::tuple<float, double, std::string> (notethat T is int).Nontype and template template arguments cannot be placed directly in the tuple (see [11]), so they arewrapped in integral constant or template template arg, respectively.integral constant is part of the type traits library from the library


vararg_templates_n1483

Download vararg_templates_n1483
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view vararg_templates_n1483 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view vararg_templates_n1483 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?