[xsd-users] Howto avoid slicing with xsd-types to std::string

Boris Kolpackov boris at codesynthesis.com
Thu Oct 14 13:09:30 EDT 2021


Tempelaar E. (Erik) <Erik.Tempelaar at vanoord.com> writes:

> " Use pointer or reference to avoid slicing from 
> "string<char, xsd::cxx::tree::simple_type<char, xsd::cxx::tree::_type>>" 
> to "basic_string<char>"
> 
>     if (cfg->Configuration().ProjectFolder().present()) {
> 
>       m_projectFolder = cfg->Configuration().ProjectFolder().get();
> 
> 
> with: m_projectFolder:
> std::string m_projectFolder{""};
> 
> <xsd:element name="ProjectFolder" type="xsd:string" minOccurs="0">
> </xsd:element>

What happens here is xsd:string XML Schema type is mapped to a C++ type 
that derives from std::string (it also contains some other things to 
implement the XML Schema builtin type hierarchy). And what you are 
doing here seems pretty normal to me: you copy the string part to 
a separate data member.

I suspect you can suppress this warning by explicitly casting to
std::string&, though that's admittedly quite ugly:

m_projectFolder = static_cast<const std::string&>(
  cfg->Configuration().ProjectFolder().get());

Another thing that may make sense to look into is if there is an 
ability  in your static analyzer to disable this warning for a 
specific.



More information about the xsd-users mailing list