使用Boost时
#include "stdafx.h"
#include "boost/thread.hpp"
#include "boost/xpressive/xpressive.hpp"
#include <iostream>
#include <string>
using namespace std;
using namespace boost::xpressive;
void mythread()
{
cout << "hello,thread!" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::function<void()> f(mythread);
boost::thread t(f);
t.join();
cout << "thread is over!" << endl;
string hello( "hello world!" );
sregex rex = sregex::compile( "(\\w+) (\\w+)!" );
smatch what;
if(regex_match( hello, what, rex ))
{
cout << what[0] << '\n'; // whole match
cout << what[1] << '\n'; // first capture
cout << what[2] << '\n'; // second capture
}
return 0;
}
编译时出错
------ 已启动生成: 项目: BoostTest, 配置: Debug Win32 ------
1> stdafx.cpp
1> BoostTest.cpp
1>e:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(2761): error C4996: 'std::_Fill_n': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> e:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(2747) : 参见“std::_Fill_n”的声明
1> d:\boost_1_55_0\boost\xpressive\detail\utility\sequence_stack.hpp(235): 参见对正在编译的函数 模板 实例化“_OutIt std::fill_n<T*,size_t,T>(_OutIt,_Diff,const _Ty &)”的引用
1> with
1> [
1> _OutIt=boost::xpressive::detail::sub_match_impl<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>> *,
1> T=boost::xpressive::detail::sub_match_impl<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>,
1> _Diff=size_t,
1> _Ty=boost::xpressive::detail::sub_match_impl<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> d:\boost_1_55_0\boost\xpressive\detail\utility\sequence_stack.hpp(233): 编译类 模板 成员函数“boost::xpressive::detail::sub_match_impl<BidiIter> *boost::xpressive::detail::sequence_stack<T>::push_sequence(size_t,const T &,boost::xpressive::detail::fill_t)”时
1> with
1> [
1> BidiIter=std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>,
1> T=boost::xpressive::detail::sub_match_impl<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> d:\boost_1_55_0\boost\xpressive\detail\core\state.hpp(292): 参见对正在编译的函数 模板 实例化“boost::xpressive::detail::sub_match_impl<BidiIter> *boost::xpressive::detail::sequence_stack<T>::push_sequence(size_t,const T &,boost::xpressive::detail::fill_t)”的引用
1> with
1> [
1> BidiIter=std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>,
1> T=boost::xpressive::detail::sub_match_impl<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> d:\boost_1_55_0\boost\xpressive\match_results.hpp(96): 参见对正在编译的类 模板 实例化“boost::xpressive::detail::sequence_stack<T>”的引用
1> with
1> [
1> T=boost::xpressive::detail::sub_match_impl<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> d:\boost_1_55_0\boost\smart_ptr\intrusive_ptr.hpp(97): 参见对正在编译的类 模板 实例化“boost::xpressive::detail::results_extras<BidiIter>”的引用
1> with
1> [
1> BidiIter=std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>
1> ]
1> d:\boost_1_55_0\boost\smart_ptr\intrusive_ptr.hpp(96): 编译类 模板 成员函数“boost::intrusive_ptr<T>::~intrusive_ptr(void)”时
1> with
1> [
1> T=boost::xpressive::detail::results_extras<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> d:\boost_1_55_0\boost\xpressive\match_results.hpp(388): 参见对正在编译的函数 模板 实例化“boost::intrusive_ptr<T>::~intrusive_ptr(void)”的引用
1> with
1> [
1> T=boost::xpressive::detail::results_extras<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> d:\boost_1_55_0\boost\xpressive\match_results.hpp(1346): 参见对正在编译的类 模板 实例化“boost::intrusive_ptr<T>”的引用
1> with
1> [
1> T=boost::xpressive::detail::results_extras<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>>
1> ]
1> e:\lx\boosttest\boosttest.cpp(28): 参见对正在编译的类 模板 实例化“boost::xpressive::match_results<BidiIter>”的引用
1> with
1> [
1> BidiIter=std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>
1> ]
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
请在stdafx.h 最上面添加#define _SCL_SECURE_NO_WARNINGS解决问题。
原因是由于Visual C++ 2012 使用了更加安全的 run-time library routines 。
错误原因解释:
这种微软的警告,主要因为那些C库的函数,很多函数内部是不进行参数检测的(包括越界类的),微软担心使用这些会造成内存异常,所以就改写了同样功能的函数,改写了的函数进行了参数的检测,使用这些新的函数会更安全和便捷。关于这些改写的函数你不用专门去记忆,因为编译器对于每个函数在给出警告时,都会告诉你相应的安全函数,查看警告信息就可以获知,在使用时也再查看一下MSDN详细了解。
参考资料:《安全模板重载》