简体   繁体   English

为什么要预先声明std :: basic_string <T> 中断boost :: regex?

[英]Why does predeclaring std::basic_string<T> break boost::regex?

Using the Microsoft Visual Studio 2012 compiler, with boost 1.53, I had some working code that used: 使用带有boost 1.53的Microsoft Visual Studio 2012编译器,我有一些使用的工作代码:

#include <boost/regex.hpp>

Later I added the following two lines to a header file that was included before regex.hpp: 后来我将以下两行添加到了regex.hpp之前的头文件中:

namespace std {template<class T> class basic_string<T>;}
typedef std::basic_string<TCHAR> tstring;

Now I get a pile of compile errors, but if regex.hpp is included before these other lines, there are no errors. 现在,我遇到了一堆编译错误,但是如果在其他这些行之前包含regex.hpp,则没有错误。

The first few of many errors are: 许多错误的前几个是:

c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2027: use of undefined type 'std::basic_string<_Elem,_Traits,_Alloc>'
19>          with
19>          [
19>              _Elem=char16_t,
19>              _Traits=std::char_traits<unsigned short>,
19>              _Alloc=std::allocator<char16_t>
19>          ]
19>c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2065: 'const_iterator' : undeclared identifier
19>c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106): error C2923: 'boost::match_results' : 'const_iterator' is not a valid template type argument for parameter 'BidiIterator'
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2825: '_Iter': must be a class or namespace when followed by '::'
19>          c:\program files (x86)\boost\boost_1_53\boost\regex\v4\iterator_traits.hpp(116) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
19>          with
19>          [
19>              _Iter=int
19>          ]
19>          c:\program files (x86)\boost\boost_1_53\boost\regex\v4\match_results.hpp(68) : see reference to class template instantiation 'boost::re_detail::regex_iterator_traits<T>' being compiled
19>          with
19>          [
19>              T=int
19>          ]
19>          c:\program files (x86)\boost\boost_1_53\boost\regex\v4\instances.hpp(106) : see reference to class template instantiation 'boost::match_results<BidiIterator>' being compiled
19>          with
19>          [
19>              BidiIterator=int
19>          ]
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2039: 'iterator_category' : is not a member of '`global namespace''
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category'
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
19>          with
19>          [
19>              _Iter=int
19>          ]
19>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
19>          with
19>          [
19>              _Iter=int
19>          ]
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
19>          with
19>          [
19>              _Iter=int
19>          ]
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2825: '_Iter': must be a class or namespace when followed by '::'
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2039: 'value_type' : is not a member of '`global namespace''
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2146: syntax error : missing ';' before identifier 'value_type'
19>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(365): error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>'

Please can anyone tell me what is wrong with my predeclaration of std::basic_string? 请任何人告诉我我的std :: basic_string预声明有什么问题吗?

You've omitted a couple of template parameters from your declaration. 您在声明中省略了几个模板参数。 The correct one looks like: 正确的外观如下:

template<class charT, class traits = char_traits<charT>,
         class Allocator = allocator<charT> >
class basic_string;

Don't try to do this on your own though. 不过,请勿尝试自行执行此操作。 Use <string> . 使用<string>

Please can anyone tell me what is wrong with my predeclaration of std::basic_string ? 请任何人告诉我我的std::basic_string预声明有什么问题吗?

It has three template parameters: 它具有三个模板参数:

template<class charT, class traits = char_traits<charT>,
    class Allocator = allocator<charT> >
class basic_string;

But there's no reason to declare standard types yourself and, as you've demonstrated, a good reason not to. 但是没有理由自己声明标准类型,而且正如您所证明的,没有理由声明。 So don't do that; 所以不要那样做。 include the header if you need the declaration. 如果需要声明,请包含标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM