Example code in Chapter 9 of Effective C++ (memo)

reference wrapper.

 

#include <tr1/functional>
#include <iostream>
using namespace std;

void Increment( int& iValue )
{
  iValue++;
}

int main(int argc, const char *argv[]) {
  int iVariable = 0;
  tr1::function< void()> fIncrementMyVariable = tr1::bind(&Increment, 
      tr1::ref( iVariable ));
  fIncrementMyVariable();
  cout << iVariable << endl;
  return 0;
}

 

boost lambda

 

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
using namespace boost::lambda;

int main(int argc, const char *argv[]) {
  std::vector<int> v;
  v.push_back(10);
  v.push_back(20);
  v.push_back(30);

  for_each(v.begin(),
           v.end(),
           cout << _1 + 100 << "\n"); 
  return 0;
}

 numeric cast

 

#include <boost/numeric/conversion/cast.hpp>
#include <iostream>
using namespace std;

int main(int argc, const char *argv[]) {
  cout << sizeof(int) << endl;
  cout << sizeof(short) << endl;
  int big = 65536 + 10;
  short little = big;
  cout << little << endl;
  short good = boost::numeric_cast<short>(big);
  return 0;
}

 boost mpl

 

#include <boost/mpl/list.hpp>
#include <boost/mpl/push_front.hpp>

int main(int argc, const char *argv[]) {
  typedef boost::mpl::list<float, double, long double> floats;
  typedef boost::mpl::push_front<floats, int>::type types;

  return 0;
}

你可能感兴趣的:(C++,c,C#)