`
yaojingguo
  • 浏览: 201690 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics