cygwin, gcc4, tr1

このソースが一応、cygwinでもビルドできた。

#include <iostream>
#include <tr1/functional>

using namespace std;

int count1(int n) {
  return ++n;
}

int count2(int &n) {
  return ++n;
}

int count3(int *n) {
  return ++(*n);
}

int main() {
  int n1 = 0, n2 = 0, n3 = 0;
  tr1::function<int (void)> f1 = tr1::bind(count1, n1);
  tr1::function<int (void)> f2 = tr1::bind(count2, n2);
  tr1::function<int (void)> f3 = tr1::bind(count3, &n3);

  cout << "f1: " << f1() << endl;
  cout << "f1: " << f1() << endl;
  cout << "f1: " << f1() << endl;
  cout << "f2: " << f2() << endl;
  cout << "f2: " << f2() << endl;
  cout << "f2: " << f2() << endl;
  cout << "f3: " << f3() << endl;
  cout << "f3: " << f3() << endl;
  cout << "f3: " << f3() << endl;
}


~/work$ g++-4 foo.cpp -o foo -Xlinker --enable-auto-import
~/work$ ./foo
f1: 1
f1: 1
f1: 1
f2: 1
f2: 2
f2: 3
f3: 1
f3: 2
f3: 3

  • ヘッダファイルに「tr1/」を付ける
  • リンカに「--enable-auto-import」オプションを渡す

「--enable-auto-import」について、調べないと。