Home > Archives > 2007年09月25日

2007年09月25日

g++ のインライン展開

どうやら g++ は varargs な関数を展開できないらしい.

#include <iostream>
 
struct h {};
struct k : h {};
 
namespace g {
  inline bool f(...) { return false; }
  inline bool f(h* ) { return true;  }
}
namespace v {
  template <typename A>
  inline bool f(A)   { return false; }
  inline bool f(h* ) { return true;  }
}
 
int main(int argc, char *argv[])
{
  h *ph; k *pk; int *pi;
  std::cout << g::f(pi) << std::endl;
  std::cout << g::f(ph) << std::endl;
  std::cout << g::f(pk) << std::endl;
  std::cout << v::f(pi) << std::endl;
  std::cout << v::f(ph) << std::endl;
  std::cout << v::f(pk) << std::endl;
}

g::f(pi) は inline bool f(int* ) なので展開される.g::f(h()) は inline bool f(...) なので展開されない.他は全部展開.ついでに,(...) を使わずに単純にテンプレート変数を一つ用意すればいいかなと思ったけど(...)とテンプレートを用いた場合とでオーバーロードの解決優先度が違うのでだめらしい.

古いソースだとbool boost::detail::function::has_empty_targetはvarargsを使ってないのになぜ最近の実装では varargs を使うようになったのだろう?

Home > Archives > 2007年09月25日

Search
Feeds

Page Top