If I write code (for example a custom container) that should satisfy a specific concept (for example an InputIterator), I have to write much boilerplate (for example method/operator signatures and typedefs) to get started (write method/operator bodies and types in typedefs). This boilerplate code could be generated by a tool and I only have to fill gaps or change some parts.

The following is an excerpt of the Iterator concept (reference):


The type It satisfies Iterator if

  • The type It satisfies CopyConstructible, and
  • The type It satisfies CopyAssignable, and
  • The type It satisfies Destructible, and
  • lvalues of type It satisfy Swappable, and
  • std::iterator_traits<It> has member typedefs value_type, difference_type, reference, pointer, and iterator_category, and


  • r, an lvalue of type It.

The following expressions must be valid and have their specified effects:

| **Expression** | **Return Type** |           **Precondition**                                               |
|    `*r`        |   unspecified   | **r** is dereferenceable (see below)                                     |
|    `++r`       |      `It&`      | **r** is incrementable (the behavior of the expression `++r` is defined) |

A tool could generate code that looks something like this:

template <typename T>
class my_iterator {
  typedef my_iterator<T> self_type;
  typedef std::input_iterator_tag iterator_category;
  typedef T value_type;
  typedef T & reference;
  typedef T * pointer;

  reference operator*() const 
    // TODO

  self_type operator++()
    // TODO

  // ...
  // more operator and method stubs of required concepts
  // ...


How can I generate such code? Which tool can I use? In the Java-World, the IDE Eclipse can "implement interfaces" (create method stubs). Is there any C++ IDE that supports a similar feature for concepts? Or are there maybe any (predefined) code snippets I can use for this purpose?

| improve this question | | | | |
  • Welcome to Software Recommendations! Please note this site is about recommending software, not assets or resources like howtos, manuals/tutorials, multi-media content, code fragments, etc. Asking for a library/tool of course is OK, so please focus on that :) – Izzy Nov 25 '16 at 9:45
  • 1
    I elaborate on my asked questions to clarify. I ask how to generate code since there might be different approaches with different pros and cons. I ask which tool because there might be different solutions of an approach. I mention code snippets as an approach that might be used. By snippet I don't mean a code sample, but an editor tool to insert a code fragment with placeholders that you can traverse (with the tool) to fill the gaps (parameters). – maiermic Nov 25 '16 at 10:19
  • One downside of snippets is that they don't observe existing code. If I already have a ForwardIterator implementation and like to extend it to a RandomAccessIterator then I don't like to generate code that already exists in my ForwardIterator implementation. However, snippets sound easier to implement since they don't need static code analysis. – maiermic Nov 25 '16 at 10:25

Our DMS Software Reengineering Toolkit can instantiate target-language surface-syntax patterns with placeholders that are themselves generated by other patterns or primitive values.

The patterns represent syntax constructs drawn from an underlying grammar for the target language, e.g., identifier, expression, declaration, statement, block, method, class or any sequence of these, with placeholders being similarly syntactically typed. The patterns are checked for syntax validity statically before any use, and well-define composition of such patterns using subpatterns for parameters are thus also syntactically well typed.

DMS has language definitions (and can therefore provide patterns for) many mainstream languages including Java, JavaScript, C# and C++14.

Op's sample could be coded as:

domain Cpp; -- tells pattern parser to use C++ grammar definition

pattern MyIterator(name:IDENTIFIER,
                   operator_star_body: method_body,
                   operator_pp_body: method_body,
                   additional_methods: class_body
                   ): template_declaration =
"   template <typename T>
    class \name {
      typedef \name<T> self_type;
      typedef std::input_iterator_tag iterator_category;
      typedef T value_type;
      typedef T & reference;
      typedef T * pointer;

      reference operator*() const 
      { \operator_star_body }

      self_type operator++()
      { \operator_pp_body }



[Forgive any C++ coding sins; I'm not a C++ template expert. It should be obvious they are easily fixed.].

The pattern has a head, declaring the pattern name and its syntax category, and the names and syntax categories of the parameters, and body in metaquotes ", whose text is parsed for validity and contains meta-escaped \parameternames. Details of the pattern language can be found here.

Typically one builds up a library of these patterns and composes them to generate big chunks of code. One also uses custom metaprogramming to generate certain types of substituted parameters, such as identifiers, etc.

One can define patterns for other elements, e.g, a method body looping over the elements of an array. Using DMS's metaprogramming langauge PARLANSE, these subpatterns can be instantiated (recursively) and then substituted for parameters in this pattern. This is all done using abstract syntax trees, and thus the final result is also an abstract syntax tree, but DMS's prettyprinter can convert this AST back into valid source text.

In addition to code generation, one can write transformation rules that map a found pattern into a replacement pattern. Massive sets of C++ patterns and transforms have been used with DMS to carry out massive reengineering of existing C++ codes.


  • Parameterized code templates whose syntax is enforced
  • Works for many langauges including C++

This is a product of my company, so this is not a recommendation. I note that it exists.

| improve this answer | | | | |
  • I like the capabilities to check syntax and prettyprint code. Does DMS ship with pre-defined patterns (for STL concepts) or are there any sources for patterns? – maiermic Nov 30 '16 at 6:02
  • DMS does not come with any predefined library of patterns. Our experience is that patterns are specific to the tasks you find important. DMS isn't a widely used tool, so there has been no time for a community ot pattern-writers to focus on an area and build libraries. The good news is that you can build patterns :-} – Ira Baxter Nov 30 '16 at 13:36

There is T4 for Visual Studio.

a T4 text template is a mixture of text blocks and control logic that can generate a text file

Since a .cpp file (and others as well) is also "a text file", this is used for code generation.

Hopefully they provide better syntax highlighting in the new versions. I last used it on VS 2010. It worked well, but coding code in code was quite un-readable that time.

<#@ template hostspecific="false" language="C#" #>  
<#@ output extension=".cpp" #>  
class my_iterator {
for (int i = 0; i<=10; i++)   
{ #>  
   int member<#= i #> = <#= i #>;
<# } #> 
} // end class
| improve this answer | | | | |
  • Does T4 have pre-defined snippets for STL concepts? – maiermic Nov 25 '16 at 16:18
  • @maiermic: I don't think so – Thomas Weller Nov 25 '16 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.