|
|
在使用Boost.Bind库时,发现如果使用构造函数创建临时变量再与bind绑定会出错。程序如下:
- #include <iostream>
- #include <string>
- #include <boost/bind.hpp>
- void print(const std::string& s)
- {
- std::cout << s << std::endl;
- }
- int main()
- {
- boost::bind(&print, _1)
- (std::string("Hello, world!"));
- }
复制代码
在编译时出现一下错误信息:
dxy@dengxy:~/Program$ g++ test.cc
test.cc: In function ‘int main()’:
test.cc:13: error: no match for call to ‘(boost::_bi::bind_t<void, void (*)(const std::string&), boost::_bi::list1<boost::arg<1> > >) (std::string)’
/usr/include/boost/bind/bind_template.hpp:17: note: candidates are: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>: perator()() [with R = void, F = void (*)(const std::string&), L = boost::_bi::list1<boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:23: note: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>: perator()() const [with R = void, F = void (*)(const std::string&), L = boost::_bi::list1<boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:29: note: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>: perator()(A1&) [with A1 = std::string, R = void, F = void (*)(const std::string&), L = boost::_bi::list1<boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:35: note: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>: perator()(A1&) const [with A1 = std::string, R = void, F = void (*)(const std::string&), L = boost::_bi::list1<boost::arg<1> >]
如果使用bind的语句修改为:
boost::bind(&print, _1)("Hello, world!");
即不构造std::string临时变量,而是使用从const char*到std::string的隐式转换就没有问题。
这里想问一下:
1、源程序是否为合法的C++程序?
2、如果源程序合法,那么这个问题是否与libstdc++的string类实现方法有关?即是否产生了引用的引用错误?
3、除以上两点外,是否会由其他因素导致?
4、可否确定这是g++的bug?
此段代码在Debian sid的g++-4.0(g++ 4.0.3 20051201 prerelease)及MinGW(g++ 3.4.4)上测试编译,出现同样的错误。在Visual C++ 2005却编译通过。 |
|