C++0x: Concepts

From Dwarf Wiki
Jump to: navigation, search

For detail description of the feature, please refer to:

http://www.research.att.com/~bs/C++0xFAQ.html#concepts

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2501.pdf


Overview

Concepts is a mechanism for describing the requirements on type and combination of types. The main motivation is to allow compiler to give better diagnostics when templates are used incorrectly.

template<ForwardIterator Iter, class V>        // Iter must be a ForwardIterator
  requires Assignable<Iter::value_type,V>      // V must be able to assign to Iter::value_type
void fill(Iter first, Iter last, const V& v);

fill (0, 9, 9.9);              // ERROR: int is not a FowardIterator
int arr[10];
fill (&arr[0], &arr[9], 9.9);  // OK: int* is a FowardIterator
fill (&arr[0], &arr[9], arr);  // ERROR: arr cannot assign into int 
                               //        (The value_type of int* is int)


Proposed change to DWARF

none.

Concepts is a mechanism for compiler to provide better compile-time diagnostics when templates are used incorrectly. It does not alter the prototype or the semantics of the template.


Example

template<ForwardIterator Iter, class V>
  requires Assignable<Iter::value_type,V>
void fill (Iter first, Iter last, const V& v);

int arr[10];
fill (&arr[0], &arr[9], 9.9);

The DWARF debug information would look like the same template declaration with the concept keywords removed:

template<typename Iter, class V>
void fill (Iter first, Iter last, const V& v);

int arr[10];
fill (&arr[0], &arr[9], 9.9);

<1><  ###>      DW_TAG_subprogram
                DW_AT_name                  fill
                DW_AT_external              yes(1)
<2><  ###>      DW_TAG_template_type_parameter
                DW_AT_name                  Iter
                DW_AT_type                  <xxx>  // int*
<2><  ###>      DW_TAG_template_type_parameter
                DW_AT_name                  V
                DW_AT_type                  <yyy>  // double
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  first
                DW_AT_type                  <xxx>  // int*
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  last
                DW_AT_type                  <xxx>  // int*
<2><  ###>      DW_TAG_formal_parameter
                DW_AT_name                  v
                DW_AT_type                  <zzz>  // const double&