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

Tempelaar E. (Erik) Erik.Tempelaar at vanoord.com
Fri Oct 22 02:49:03 EDT 2021


Thanks for looking into it. I figured this much, but I was hoping there was a library-call that explicitly returns the std::string that I had missed.
Good to know it is pretty normal.

I was able to resolved the issues as "won't fix" and I feel like that's the best solution here.

Thanks again.

-----Original Message-----
From: Boris Kolpackov <boris at codesynthesis.com>
Sent: 14 October 2021 19:10
To: Tempelaar E. (Erik) <Erik.Tempelaar at vanoord.com>
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] Howto avoid slicing with xsd-types to std::string

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.




Disclaimer: This mail transmission and any attached files are confidential and are intended for the addressee only. If you are not the person or organization to whom it is addressed, you must not copy, disclose, distribute or take any action in reliance upon it. If you have received this message in error, please contact the sender by email and delete all copies of this message and all copies of any attached files.



More information about the xsd-users mailing list