limitations.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>Limitations - Boost.Outcome documentation</title>
  4. <link rel="stylesheet" href="../../css/boost.css" type="text/css">
  5. <meta name="generator" content="Hugo 0.52 with Boostdoc theme">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  7. <link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../experimental/c-api.html"><img src="../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../experimental/c-api.html"><img src="../../images/up.png" alt="Up"></a>
  11. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/c-api/example.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Limitations</h1></div></div></div>
  13. <p>C++ has excellent two-way compatibility with the C ABI, but there are some
  14. limitations you must observe to write C++ code which C code can call without
  15. marshalling at the ABI boundary:</p>
  16. <ol>
  17. <li><p>A C++ function may not throw exceptions if it is safe to call from C, and
  18. so should always be marked <code>noexcept</code>.</p></li>
  19. <li><p>A C++ function should be annotated with <code>extern &quot;C&quot;</code> to prevent its symbol
  20. being mangled, and thus give it the C rather than C++ ABI.</p></li>
  21. <li><p>You cannot use overloading in your <code>extern &quot;C&quot;</code> functions.</p></li>
  22. <li><p>You may only use types in your C++ function declaration for which these traits are both true:</p>
  23. <ul>
  24. <li><a href="http://en.cppreference.com/w/cpp/types/is_standard_layout"><code>std::is_standard_layout_v&lt;T&gt;</code></a></li>
  25. <li><a href="http://en.cppreference.com/w/cpp/types/is_trivially_copyable"><code>std::is_trivially_copyable_v&lt;T&gt;</code></a></li>
  26. </ul>
  27. <p>(Note that <code>std::is_trivially_copyable_v&lt;T&gt;</code> requires trivial destruction,
  28. but NOT trivial construction. This means that C++ can do non-trivial construction
  29. of otherwise trivial types)</p></li>
  30. </ol>
  31. <hr />
  32. <p>The above is what the standard officially requires for <em>well defined</em> C and C++ interop.
  33. However, all of the three major compilers MSVC, GCC and clang are considerably more relaxed.
  34. In those three major compilers, &ldquo;almost-standard-layout&rdquo; C++ types work fine in C.</p>
  35. <p>&ldquo;Almost-standard-layout&rdquo; C++ types have these requirements:</p>
  36. <ol>
  37. <li>No virtual functions or virtual base classes i.e.
  38. <a href="http://en.cppreference.com/w/cpp/types/is_polymorphic"><code>std::is_polymorphic_v&lt;T&gt;</code></a>
  39. must be false. This is because the vptrs offset the proper front of the data layout
  40. in an unknowable way to C.</li>
  41. <li>Non-static data members of reference type appear to C as pointers. You
  42. must never supply from C to C++ a non-null pointer which is seen as a reference in C++.</li>
  43. <li>C++ inheritance is seen in C data layout as if the most derived class has nested
  44. variables of the inherited types at the top, in order of inheritance.</li>
  45. <li>Types with non-trivial destructors work fine so long as at least move construction
  46. and assignment is the same as
  47. copying bits like <code>memcpy()</code>. You just need to make sure instances of the type return
  48. to C++, and don&rsquo;t get orphaned in C. This was referred to in previous pages in this
  49. section as &ldquo;move relocating&rdquo; types.</li>
  50. </ol>
  51. <p>Experimental Outcome&rsquo;s support for being used from C does not meet the current strict
  52. requirements, and thus relies on the (very common) implementation defined behaviour just
  53. described (it is hoped that future C++ standards can relax the requirements to those
  54. just described).</p>
  55. <p>Specifically, proposed <code>status_code</code> is an almost-standard-layout type,
  56. and thus while it can&rsquo;t be returned from <code>extern &quot;C&quot;</code> functions as the compiler
  57. will complain, it is perfectly safe to return from C++ functions to C code on the
  58. three major compilers, as it is an &ldquo;almost-standard-layout&rdquo; C++ type if <code>T</code> is
  59. an &ldquo;almost-standard-layout&rdquo; C++ type.</p>
  60. </div><p><small>Last revised: February 05, 2019 at 17:14:18 UTC</small></p>
  61. <hr>
  62. <div class="spirit-nav">
  63. <a accesskey="p" href="../../experimental/c-api.html"><img src="../../images/prev.png" alt="Prev"></a>
  64. <a accesskey="u" href="../../experimental/c-api.html"><img src="../../images/up.png" alt="Up"></a>
  65. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/c-api/example.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  66. </html>