Generic programming techniques for boost applications

Source: Internet
Author: User

1. What is generic programming

Generic Programming (Generic programming) focuses on generating common software components that can be easily reused in different applications. In C + +, class templates and function templates are very effective mechanisms for generics programming. With these two great weapons, we do not need to pay the cost of efficiency while realizing the generics.

As a simple example of generic programming, let's look at how to make the memcpy () function generic in the C library. One way to implement this might be:

void* memcpy(void* region1, const void* region2, size_t n)
{
  const char* first = (const char*)region2;
  const char* last = ((const char*)region2) + n;
  char* result = (char*)region1;
  while (first != last)
    *result++ = *first++;
  return result;
}

This memcpy () function has been generalized to some extent by using void* so that the function can copy different types of arrays. But what if the data we want to copy isn't in an array, but is stored by a list? Can we extend the concept so that it copies arbitrary sequences? Look at the function body of the memcpy (), which has a minimum requirement for the incoming sequence _: It needs to traverse the sequence with some form of pointer, access the element to which the pointer is pointing, write the element to the destination, and compare the pointer to determine when to stop the copy. The C + + standard library groups such requirements, called Concepts (concepts). In this example there are two concepts of input iterators (corresponding to Region1) and output iterators (corresponding to Region2).

If we rewrite memcpy () with a function template, using input iterators and output iterators as template parameters to describe the need for sequences, we can write a copy () function with higher reusability:

template <typename InputIterator, typename OutputIterator>
OutputIterator
copy(InputIterator first, InputIterator last, OutputIterator result)
{
  while (first != last)
    *result++ = *first++;
  return result;
}

Using this generic copy () function, we can copy a variety of sequences as long as it meets the requirements we specify. An external list of iterators, such as Std::list, can also be copied by our copy () function:

#include <list>
#include <vector>
#include <iostream>
int main()
{
  const int N = 3;
  std::vector<int> region1(N);
  std::list<int> region2;

  region2.push_back (1);
  region2.push_back(0);
  region2.push_back(3);

  std::copy(region2.begin(), region2.end(), region1.begin ());

  for (int i = 0; i < N; ++i)
     std::cout << region1[i] << " ";
   std::cout << std::endl;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.