c++ - Use of std::string_view in extern "C" DLL - Stack Overflow

admin2025-04-06  0

I am designing a C++ DLL with extern "C" bindings for use in Python and C#. Some of the structures passed around currently contain C strings that I'm considering to change to std::string_view for following benefits:

  1. They are clearly non-owning (user of DLL does not call free)
  2. They are of known size, so user does not have to check it with strlen
  3. Because of that, they don't have to be null terminated, making substrings cheaper to create for DLL

Is std::string_view (and std::span<T> which is pretty much the same thing) guaranteed to be exactly { T*; size_t; }, or am I safer to create my own struct for it to maintain ABI across languages? cppreference lists "data members" which suggests the layout is guaranteed by the standard.

I am designing a C++ DLL with extern "C" bindings for use in Python and C#. Some of the structures passed around currently contain C strings that I'm considering to change to std::string_view for following benefits:

  1. They are clearly non-owning (user of DLL does not call free)
  2. They are of known size, so user does not have to check it with strlen
  3. Because of that, they don't have to be null terminated, making substrings cheaper to create for DLL

Is std::string_view (and std::span<T> which is pretty much the same thing) guaranteed to be exactly { T*; size_t; }, or am I safer to create my own struct for it to maintain ABI across languages? cppreference lists "data members" which suggests the layout is guaranteed by the standard.

Share Improve this question edited Apr 1 at 20:45 Alan Birtles 36.7k4 gold badges37 silver badges68 bronze badges asked Apr 1 at 20:32 Dominik KaszewskiDominik Kaszewski 9355 silver badges23 bronze badges 5
  • 3 If you're exporting c++ functions you shouldn't be using extern "C". The abi of all standard library types is defined by the standard library implementation and won't be at all portable between different standard libraries – Alan Birtles Commented Apr 1 at 20:46
  • 3 The "members" listed by cppreference are for "exposition only" i.e. only for the purposes of explaining, or helping to understand. In this case, it shows one possible implementation approach, which may or may not be "best" or recommended. – Peter Commented Apr 1 at 20:50
  • 2 "cppreference lists "data members" which suggests the layout is guaranteed by the standard" - no. It only lists what members are available, but neither cppreference nor the standard say anything about the layout, that is left to the implementation to decide. – Remy Lebeau Commented Apr 1 at 20:56
  • @AlanBirtles That was basically my question, whether any of STL classes has standard-guaranteed layout so that it could be used as a C POD. All answers clearly say no. – Dominik Kaszewski Commented Apr 1 at 21:12
  • 1 @DominikKaszewski There are exceptions. See std::complex. – Ted Lyngmo Commented Apr 2 at 5:38
Add a comment  | 

1 Answer 1

Reset to default 6

Is std::string_view (and std::span<T> which is pretty much the same thing) guaranteed to be exactly { T*; size_t; }

No, you have no such guarantees. gcc for example declares the std::basic_string_view members in this order:

size_t        _M_len;
const _CharT* _M_str;

or am I safer to create my own struct for it to maintain ABI across languages?

Yes, that would be the safest.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743872761a222189.html

最新回复(0)