Difference between revisions of "C++0x: Variadic templates"

From Dwarf Wiki
Jump to: navigation, search
(New page: For detail description of the feature, please refer to: [http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2087.pdf] [http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2080.p...)
 
Line 1: Line 1:
 
For detail description of the feature, please refer to:
 
For detail description of the feature, please refer to:
[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2087.pdf]
+
 
[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2080.pdf]
+
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2087.pdf
 +
 
 +
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2080.pdf
 +
 
  
 
== Overview ==
 
== Overview ==

Revision as of 16:21, 8 January 2009

For detail description of the feature, please refer to:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2087.pdf

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2080.pdf


Overview

C++0x will allow function and class templates to take an arbitrary number of arguments. The motivation behind this language extension is to allow TR1 facilities such as tuple to accept variable number of parameters.

template<typename... Elements> class tuple;
tuple<int, long> tuple_int_long;  // Elements is a template type parameter pack 
                                  // containing 2 arguments, int and long type
tuple<float> tuple_float;         // Elements is a template type parameter pack
                                     containing 1 argument: float type

template<typename T, unsigned PrimaryDimension, unsigned... Dimensions> class array;
array<double, 3, 3> t1;           // Dimensions is a template value parameter pack
                                  // containing a single argument: unsigned int 3.
array<double, 3, 3, 4, 5> t2;     // Dimensions is a template value parameter pack
                                  // containing three arguments: unsigned int 3, 4 and 5.

Proposed change to DWARF

Purpose

  • DWARF does not represent the generic template definition, but does represent each template instantiation.
  • Template type parameter pack contains only template type parameters
  • Template value parameter pack contains only template value parameters
  • Debug information should show where the template parameter pack is used within the template definition.
  • If possible, debug information should also show the arguments within the template parameter pack.


New DWARF tag

DW_TAG_template_parameter_pack 0x43 template parameters pack

Allowable attributes:

  • DECL
  • DW_AT_name
  • DW_AT_type
  • DW_AT_sibling


New DWARF attribute

DW_AT_parameter_pack 0x6A flag an argument within template parameters pack


3.3.7: Function Template Instantiations

Each template parameter pack declaration appearing in the template definition is represented by a debugging information entry with the tag DW_TAG_template_parameter_pack. Each such entry may have a DW_AT_name attribute, whose value is a null-terminated string containing the name of the template parameter pack as it appears in the source program.

Template parameter pack entries can have zero or more child entries. Each entry correspond to exactly one argument within the template parameter pack. This child entry must have the tag DW_TAG_template_type_parameter, and will have same form as other template type parameter entries.


5.6.8: Class Template Instantiations

Each template parameter pack declaration appearing in the template definition is represented by a debugging information entry with the tag DW_TAG_template_parameter_pack. Each such entry may have a DW_AT_name attribute, whose value is a null-terminated string containing the name of the template parameter pack as it appears in the source program.

If the template parameter pack entry represent a template value parameter pack, it may contain a DW_AT_type attribute to denote the type of the arguments within the template value parameter pack.

Template parameter pack entries can have zero or more child entries. Each entry correspond to exactly one argument within the template parameter pack. This child entry can have one of the tags DW_TAG_template_type_parameter or DW_TAG_template_value_parameter, and will have same form as other template parameter entries.


4.1: Data Object Entries

If a formal parameter is an argument within the template parameter pack, the formal parameter entry has a DW_AT_parameter_pack attribute, whose value is a flag.


Example

void printf(const char* s) { }
template<typename T, typename... Args>
void printf(const char* s, T value, Args... args) {
...
  printf (s, args...);
...  
}

int x;
printf<int, char, int> ("%c %d", x, 'x', 3);

Compiler may instantiate these 4 functions as a result:

  1. printf<int,char,int> (const char* s, int, Args...);
  2. printf<char,int> (const char* s, char, Args...);
  3. printf<int> (const char* s, int, Args...);
  4. printf (const char* s);
1) printf<int,char,int> (const char* s, int, Args...);
<1><  ###>      DW_TAG_subprogram
                DW_AT_name                  printf
<2><  ###>      DW_TAG_template_type_parameter
                DW_AT_name                  T
                DW_AT_type                  <xxx>  // int
<2><  ###>      DW_TAG_template_parameter_pack
                DW_AT_name                  Args
<3><  ###>      DW_TAG_template_type_parameter
                DW_AT_type                  <yyy>  // char
<3><  ###>      DW_TAG_template_type_parameter
                DW_AT_type                  <xxx>  // int
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  s
                DW_AT_type                  <zzz>  // const char*
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  value
                DW_AT_type                  <xxx>  // int
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  args
                DW_AT_parameter_pack        yes
                DW_AT_type                  <yyy>  // char
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  args
                DW_AT_parameter_pack        yes
                DW_AT_type                  <xxx>  // int

2) printf<char,int> (const char* s, char, Args...);
<1><  ###>      DW_TAG_subprogram
                DW_AT_name                  printf
<2><  ###>      DW_TAG_template_type_parameter
                DW_AT_name                  T
                DW_AT_type                  <yyy>  // char
<2><  ###>      DW_TAG_template_parameter_pack
                DW_AT_name                  Args
<3><  ###>      DW_TAG_template_type_parameter
                DW_AT_type                  <xxx>  // int
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  s
                DW_AT_type                  <zzz>  // const char*
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  value
                DW_AT_type                  <yyy>  // char
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  args
                DW_AT_parameter_pack        yes
                DW_AT_type                  <xxx>  // int

3) printf<int> (const char* s, int, Args...);
<1><  ###>      DW_TAG_subprogram
                DW_AT_name                  printf
<2><  ###>      DW_TAG_template_type_parameter
                DW_AT_name                  T
                DW_AT_type                  <xxx>  // int
<2><  ###>      DW_TAG_template_parameter_pack
                DW_AT_name                  Args
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  s
                DW_AT_type                  <zzz>  // const char*
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  value
                DW_AT_type                  <xxx>  // int

4) printf (const char* s);
<1><  ###>      DW_TAG_subprogram
                DW_AT_name                  printf
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  s
                DW_AT_type                  <zzz>  // const char*