开发者

Can I use std::basic_string with things that aren't character types?

I've got a loop in my code that uses std::basic_string<HANDLE>, and then waits on it like this:

DWORD dwWaitResult = WaitForMultipleObjects((DWORD)handles.size(),
                                            handles.data(),
                                            FALSE, POLL_INTERVAL_MS);

It works fine, but when I turn on /W4 and /analyze, Visual C++ 2008 warns with the following (cut down and wrapped for brevity):

iosfwd(266) : warning C6001: Using uninitialized memory '*_First'
iosfwd(262) : while compiling class template member function
       'HANDLE *std::char_traits<_Elem>::assign(_Elem *,size_t,_Elem)'
        with [ _Elem=HANDLE ]
xstring(2155) : see reference to class template instantiation
       'std::char_traits<_Elem>' being compiled
        with [ _Elem=HANDLE ]
xstring(2148) : while compiling class template member function
       'void std::basic_string<_Elem>::_Tidy(bool,unsigned int)'
        with [ _Elem=HANDLE ]
.\MyCode.cpp(231) : see reference to class template instantiation
       'std::basic_string<_Elem>' being compiled
        with [ _Elem=HANDLE ]

iosfwd(222) : warning C6001: Using uninitia开发者_如何学Pythonlized memory '*_First1'
iosfwd(216) : while compiling class template member function
        'HANDLE *std::char_traits<_Elem>::_Copy_s
               (_Elem *,size_t,const _Elem *,size_t)'
        with [ _Elem=HANDLE ]

Questions:

  1. Is it legal to use std::basic_string with something that's not a simple character type (i.e. char or wchar_t)?
  2. If it is, what should I do to get rid of these warnings? Answering #pragma warning(disable) needs justification.
  3. If it's not, why not? And what should I use instead?

Extra credit will be given for bearing in mind that Boost is out, and we're restricted to the STL implementation in Visual C++ 2008; the Visual C++ bits of TR1 are allowed.


whats wrong with using a std::vector instead of trying to jam that square peg into the round hole?


  1. Yes, if you provide a template specialization of the class traits for your type.

  2. Provide your specialized class traits. (Second template parameter of class std::basic_string).

  3. It is. But have you thought about using std::vector instead of std::basic_string? (It does not have this traits template argument that provides string helper functions such as compare)


Please be careful, std::basic_strings are never guaranteed to be contiguous memory, which WaitForMultipleObjects will never know about. I highly recommend using vectors instead.


Looks like you need to provide a specialisation of the char_traits template for your HANDLE type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜