g++ 4.2.3のバグ?std::mapやstd::pairをメンバ関数のデフォルト引数に書くと怒られる件
http://groups.google.co.jp/group/gnu.g++.bug/browse_thread/thread/09f52263bb8562a6
これと同じ。
#include <iostream> #include <map> using namespace std; void f(const map<int, int>& m = map<int, int>()) { } class A { public: A(const map<int, int>& m = map<int, int>()) {} void f(const map<int, int>& m = map<int, int>()) {} }; int main() { }
test.cpp:11: error: expected ‘,’ or ‘...’ before ‘>’ token test.cpp:12: error: expected ‘,’ or ‘...’ before ‘>’ token test.cpp:11: error: wrong number of template arguments (1, should be 4) /usr/include/c++/4.2/bits/stl_map.h:93: error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’ test.cpp:11: error: default argument missing for parameter 2 of ‘A::A(const std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >&, int)’ test.cpp:12: error: wrong number of template arguments (1, should be 4) /usr/include/c++/4.2/bits/stl_map.h:93: error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’ test.cpp:12: error: default argument missing for parameter 2 of ‘void A::f(const std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >&, int)’
メンバ関数のデフォルト引数に2個以上のtemplate引数を取るものを書くと、","以降を別の引数だと解釈してしまっているのかな?マクロのように。よくわからん。仮にそうだったとしたら、
A(const map<int, int>& m = (map<int, int>())) {}
このようにデフォルト引数をカッコで囲んであげればOKだと思ったので、試してみたところコンパイルできた。うーむ。これはバグだよなぁさすがに・・・。Macのg++や、他のバージョンでは大丈夫だったりもする模様。謎す。