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:
- Is it legal to use std::basic_string with something that's not a simple character type (i.e.
char
orwchar_t
)? - If it is, what should I do to get rid of these warnings? Answering
#pragma warning(disable)
needs justification. - 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?
Yes, if you provide a template specialization of the
class traits
for your type.Provide your specialized
class traits
. (Second template parameter ofclass std::basic_string
).It is. But have you thought about using
std::vector
instead ofstd::basic_string
? (It does not have this traits template argument that provides string helper functions such as compare)
Please be careful, std::basic_string
s 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.
精彩评论