以下のソースをコンパイルするとエラーが出るのは何故なんでしょうか。
コメントアウトした部分のようにすれば大丈夫なんですが。
ソース:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
using namespace std;
int func1(int n)
{
return n;
}
int func2(int n1, int n2)
{
return n1+n2;
}
class Test
{
public:
template <class T>
Test(T fn)
{
cout << fn(1) << endl;
}
};
続き。
int main()
{
Test t1(boost::function<int (int)>(boost::bind(func1, _1)));
Test t2(boost::function<int (int)>(boost::bind(func2, 10, _1)));
//boost::function<int (int)> f1(boost::bind(func1, _1));
//boost::function<int (int)> f2(boost::bind(func2, 10, _1));
//Test t1(f1);
//Test t2(f2);
return 0;
}
エラーメッセージ:
>g++ t107.cpp
t107.cpp: In function `int main()':
t107.cpp:29: redefinition of `boost::function<int ()(int), std::allocator<void>
> boost::bind'
t107.cpp:28: `boost::function<int ()(int), std::allocator<void> > boost::bind'
previously declared here
t107.cpp:29: declaration of `boost::function<int ()(int), std::allocator<void>
> boost::bind'
t107.cpp:28: conflicts with previous declaration `boost::function<int ()(int),
std::allocator<void> > boost::bind'
補足。
コンパイラは g++ (GCC) 3.2.3 (mingw special 20030504-1) です。
865 :
デフォルトの名無しさん:03/07/28 03:51
int bind(int);
class function
{
public:
function(int);
};
class Test
{
public:
Test(function);
};
int main()
{
Test t1(function(bind(0)));
Test t2(function(bind(1)));
return 0;
}
: In function `int main()':
:15: redeclaration of `function bind'
:14: `function bind' previously declared here
:15: declaration of `function bind'
:14: conflicts with previous declaration `function bind'
とりあえず、問題を局所化してみた。
boostもtemplateも関係ないらしい。
gccのバグかな。
おもしろそうだから勝手にageさせてもらった。
ちなみに、
Test t1((function(bind(0))));
Test t2((function(bind(1))));
こうすれば、宣言と式の曖昧さが解決されて、
意図しているであろう本来の意味が、gccに伝わる。
関数宣言と思われるからでは?
バグではなく、規格通りかと。