<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>A Proposal to add Classes and Functions Required for Dynamic Library Load</title>
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
		<meta http-equiv="Content-Language" content="en-us">

		<style type="text/css">
			.addition { color: green; }
			.right { float:right; }
			.changed-deleted { background-color: #CFF0FC ; text-decoration: line-through; display: none; }
			.addition.changed-deleted { color: green; background-color: #CFF0FC ; text-decoration: line-through; text-decoration: black double line-through; display: none; }
			.changed-added { background-color: #DFF0FC ;}
            pre { line-height: 1.2; font-size: 10pt; margin-top: 25px;}
            .desc { margin-left: 35px; margin-top: 10px; padding:0; white-space: normal; }
            body {max-width: 1024px; margin-left: 25px;}
            /*code {font-size: large; }*/
            .cppkeyword { color: blue; }
            .cppcomment { color: green; }
            .cppcomment > .cppkeyword{ color: green; }
            .cpptext { color: #2E8B57; }
		</style>

	</head>
	<body bgcolor="#ffffff">
		<address>Document number: P0275R4</address>
		<address>Project: Programming Language C++</address>
		<address>Audience: Library Evolution Working Group</address>
		<address>&nbsp;</address>
		<address>Antony Polukhin, Yandex.Taxi Ltd, &lt;<a href="mailto:antoshkka@gmail.com">antoshkka@gmail.com</a>&gt;, &lt;<a href="mailto:antoshkka@yandex-team.ru">antoshkka@yandex-team.ru</a>&gt;</address>
		<address>&nbsp;</address>
		<address>Date: 2018-10-01</address>
		<h1>A Proposal to add Classes and Functions Required for Dynamic Library Load</h1>


<!--span class='changed-added'>Changes to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0275r1.html">P0275R1</a> and  <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0275r2.html">P0275R2</a> are marked with blue.</span>
<input type="checkbox" id="show_deletions" onchange="show_hide_deleted()"> Show deleted lines.

<br><br><br><br-->
<div class="right">
<p>“This is just a means to get a pointer to a function.”</p>
<p><i>― Ville Voutilainen</i></p>
</div>
<br><br><br><br><br><br>

		<h2>I. Introduction and Motivation</h2>
		<p>Adding a specific features to an existing software applications at
            runtime could be useful in many cases. Such extensions, or plugins,
            are usually implemented using shared library files (.dll,
            .so/Dynamic Shared Objects) loaded into the memory of program at runtime. Such trechique is called <b>Dynamic
            Loading (DL)</b>.</p>
		<p>Current C++ Standard lacks support for Dynamic Library 
            Loading. This proposal attempts to fix that and provide a simple to
            use classes and functions for DL.</p>
		<p>A quick survey at one of <a href="https://habrahabr.ru/company/yandex/blog/323972/">the popular Russian IT sites</a> shows that ~41% of people use
		dynamic loading in company or their own projects. Any wrapper around platform specific DL involves multiple macro, reinterpret_casts,
	    work with platform specific decorations, and even UBs due to
		OS API designs. Examples: <a href="https://github.com/STEllAR-GROUP/hpx/blob/release/hpx/util/plugin/detail/dll_dlopen.hpp">HPX</a>, <a href="https://github.com/pocoproject/poco/tree/develop/Foundation/src">POCO (see SharedLibrary* files)</a>, <a href="https://github.com/qt/qtbase/blob/5.3/src/corelib/plugin/">Qt(see qlibrary_*.cpp files)</a>.
		</p>
		<p>This proposal is based on the <a href="https://github.com/boostorg/dll">Boost.DLL</a> library. <span>Unlike many other libraries the Boost.DLL
		does not force a specific type for the imported symbols, does not include project specific stuff into the classes. It's just a
		abstraction around platform specific calls that attempts to unify DL behavior.</span></p>


		<h2>II. Impact on the Standard</h2>
		<p>This proposal is a pure library extension. It does not propose changes to
			existing headers.
		</p>
		<p>This proposal is a library only extension and avoids adding a description of shared libraries machinery as much as possible.
		EWG has to decide:</p>
		<ul>
		    <li>is it acceptable to describe usage of shared library files without any mention of how to construct them?</li>
		    <li>is it acceptable to have an unspecified behavior for DLL's TLS, globals, ADL, exceptions, ODR violations, global allocators?</li>
		</ul>
		<p>EWG voted on "Add DL support to standard library?" in Albuquerque: 6 | 6 | 4 | 1 | 1, asked to embed feedback and send the proposal to LEWG.<p>

		<h2>III. Design Decisions</h2>
		<h3>A. Usage of <code>std::filesystem::path</code></h3>
		<p>To simplify library usage <code>std::filesystem::path</code> is used for specifying paths . All the <code>path</code> related overhead is minor,
            comparing to the time of loading shared library file into the memory of program or getting information about shared library file location.</p>
		<h3>B. Stick to the Filesystem error reporting scheme.</h3>
		<p>Provide two overloads for some functions, one that throws an exception to report system errors, and another that sets an <code>error_code</code>. This supports two common use cases:</p>
            <ul>
                <li>Uses where shared library file related system errors are truly exceptional and indicate a serious failure.</li>
                <li>Uses where shared library file related errors are routine and do not necessarily represent failure.</li>
            </ul>
		<h3>C. Do not take care of mangling.</h3>
		<p>C++ symbol mangling depend on compiler and platform. Existing attempts to get mangled symbol by unmangled name
        result in significant complexity and memory usage growth and overall slowdown of getting symbol from shared library file.
        </p>
        <pre>[ Example:
    // Loading a shared library file with mangled symbols
    mangled_library lib("libmycpp.so");

    // Attempt to get function "foo::bar" with signature void(int)
    lib.get&lt;void(int)&gt;("foo::bar");

    // The problem is that we do not know what `foo::bar` is:
    // * `foo` could be a class and `bar` could be a static member function
    // * `foo` could be a struct and `bar` could be a static member function
    // * `foo` could be a namespace and `bar` could be function in that namespace
    //
    // We also do not know the calling convention for `foo` and it's noexcept specification.
    //
    // Mangling of `foo::bar` depends on all the knowledge from above, so we are either forced to
    // mangle and try to obtain all the available combinations; or we need to investigate all the
    // symbols exported by "libmycpp.so" shared library file and find a best match.
- end example ]</pre>
            <p>While no good solution was found for obtaining mangled 
                symbol by unmangled name, this proposal concentrates on obtaining symbols 
                by exact name match.</p>
		<h3>D. Drop the <code>import</code> function form Boost.DLL.</h3>
			<p>While those function could simplify code development for some users, they are simple to missuse:</p>
			<pre><code>try {
    auto f = dll::import&lt;int()&gt;(path_to_pugin, "function");
    f();
    // `f` goes out of scope along, plugin is unloaded, the exception::what() code is unreachable
} catch (const std::exception&amp; e) {
    std::cerr &lt;&lt; e.what();
}
            </code></pre>
		<h3>E. Do not search libraries in system specific paths by default.</h3>
			<p>Searching paths for a specified shared library file may affect load times and 
            make the proposed wording less efficient. It is assumed to be the most 
            common case, that
            user exactly knows were the desired shared library file is
            located and provides either absolute or relative to current
            directory path. For that case searching
            system specific paths affects performance and increases the 
            chance of finding wrong shared library file with the same name. Because some 
            operating systems search
            system paths even if relative path is provided, the 
            requirement to not do that is implicitly described in proposed wording. That OS specific logic could be avoided by implicitly converting relative paths to absolute.</p>
		<h3>F. Minimal modifications.</h3>
        <p>There have been DL related proposals:</p>
        <ul>
        <li><a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2002/n1400.html">N1400: Toward standardization of dynamic libraries</a></li>
        <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1418.html">N1418: Dynamic Libraries in C++</a></li>
        <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1428.html">N1428: Draft Proposal for Dynamic Libraries in C++</a></li>
        <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1496.html">N1496: Draft Proposal for Dynamic Libraries in C++ (Revision 1)</a></li>
        <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1976.html">N1976: Dynamic Shared Objects: Survey and Issues</a></li>
        <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2015.pdf" >N2015: Plugins in C++</a></li>
        <li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2407.html">N2407: C++ Dynamic Library Support</a></li>
        </ul>

        <p>The proposal you're reading attempts to avoid issues of the proposals from above, minifying the changes to the C++ Standard. The proposal you're reading:</p>
        <ul>
        <li>is a pure library extension (does not modify Basic Concepts [basic] and does not add new keywords)</li>
        <li>does not add platform requirement for shared libraries support</li>
        </ul>

        <h3>G. Overloads take <code>const char*</code> and <code>const string&amp;</code>, not <code>string_view</code>.</h3>
        <p>OS specific API usually accepts symbol name as a zero terminated char array. <code>string_view</code> has no zero termination
        guarantee so it requires copying of the input parameter and zero termination. <code>char*</code> and <code>string&amp;</code> is a more efficient API design.</p>

		<h2>IV. Proposed wording relative to N4762</h2>

<h3>?? Dynamic loading support library <span class="right">[dl]</span></h3>
<h4>??.1 General <span class="right">[dl.general]</span></h4>
<p>
    <i>Shared library file</i> is a file containing objects and functions available for use at program startup or at runtime.
</p>
<p>
    <i>Dynamic loading</i> is a runtime mechanism to load shared library file into the memory of current program at any point of <code>main</code> [basic.start.main] execution,
    retrieve the addresses of functions and objects contained in the shared library file, execute those functions or access those objects,
    unload the shared library file from memory.
</p>
<p>
    Header &lt;experimental/dl&gt;
    defines classes and functions suitable for dynamic loading. For
    that header term <i>symbol</i> relates to a
    function, reference, class member or object that can be obtained from shared library file at runtime.
    Term <i>symbol name</i> relates to a NTBS identifier of a symbol, using
    which symbol can be obtained from shared library file. <i>[ Note:</i> For symbols declared with <code>extern
    "C"</code> in code of shared library file, symbol name is the name of the
    entity. Symbol name for entities without <code>extern "C"</code> are unspecified. Typically symbol names of those entities are mangled entity names<i> — end note ]</i>.
</p>

<h4>??.2 Error reporting <span class="right">[dl.errors]</span></h4>
<p>
    Functions not having an argument of type <code>error_code&amp;</code> report errors as follows, unless otherwise specified:</p>
    <ul>
    <li>When a call by the implementation to an operating system or other underlying API results in an error that
    prevents the function from meeting its specifications, an exception of type system_error is thrown.</li>

    <li>Failure to allocate storage is reported by throwing an exception as described in the C++ standard
    [res.on.exception.handling].</li>

    <li>Destructors throw nothing.</li>
    </ul>

    Functions having an argument of type <code>error_code&amp;</code> report errors as follows, unless otherwise specified:
    <ul>
    <li>If a call by the implementation to an operating system or other underlying API results in an 
    error that prevents the function from meeting its specifications, the <code>error_code&amp;</code> argument is set as
    appropriate for the specific error. Otherwise, <code>clear()</code> is called on the <code>error_code&amp;</code> argument.</li>

    <li>Failure to allocate storage is reported by throwing an exception
         as described in the C++ standard [res.on.exception.handling].</li>
    </ul>


<h4>??.3 Header &lt;experimental/dl&gt; <span class="right">[dl.dl]</span></h4>
<pre>namespace std {
  namespace experimental {
  inline namespace dl_v1 {
    // ??.4, shared_library
    class shared_library;

    // ??.5, runtime path functions
    template&lt;class T&gt;
    filesystem::path symbol_location(const T&amp; symbol, error_code&amp; ec);
    template&lt;class T&gt;
    filesystem::path symbol_location(const T&amp; symbol);

    filesystem::path this_line_location(error_code&amp; ec);
    filesystem::path this_line_location();

    filesystem::path program_location(error_code&amp; ec);
    filesystem::path program_location();
  }
  }

  // ??.6, hash support
  template &lt;class T&gt; struct hash;
  template &lt;&gt; struct hash&lt;experimental::shared_library&gt;;
}
</pre>


<h4>??.4 Class shared_library <span class="right">[dl.shared_library]</span></h4>
<p>
    The class <code>shared_library</code> provides means for dynamic loading.
</p>
<p>
    <code>shared_library</code> instances share reference count to an actual shared library file loaded in memory
    of current program, so it is safe and memory efficient to have multiple instances
    of <code>shared_library</code> referencing the same shared library file even if those instances
    were loaded in memory using different paths (relative and absolute) referencing the
    same shared library file.
</p>
<p>
    <i>[ Note:</i>
    It is safe to concurrently load shared library files into memory of program, unload and get symbols from
    any shared library files using different <code>shared_library</code> instances. If current
    platform does
    not guarantee safe concurrent work with shared library files, calls to <code>shared_library</code> functions are serialized by <code>shared_library</code> implementation.
    <i>— end note ]</i>
</p>
<p>
    <i>[ Note:</i>
    Constructors, comparisons and <code>reset()</code> functions that accept <code>nullptr_t</code> are not provided because that cause confusion:
    some of the platforms provide interfaces for shared library files that accept <code>nullptr_t</code> to get a handler to the current program.
    <i>— end note ]</i>
</p>
<pre>namespace std {
  namespace experimental {
  inline namespace dl_v1 {

    class shared_library {
    public:
      using native_handle_type = <i>implementation-defined</i>;

      // ??.4.1, dl_mode
      using dl_mode = <i>T1</i>;

      // ??.4.2, dl_mode constants
      static constexpr dl_mode default_mode;
      static constexpr dl_mode rtld_lazy;
      static constexpr dl_mode rtld_now;
      static constexpr dl_mode rtld_global;
      static constexpr dl_mode rtld_local;
      static constexpr dl_mode add_decorations;
      static constexpr dl_mode search_system_directories;

      // ??.4.3, construct/copy/destruct
      constexpr shared_library() noexcept;
      shared_library(shared_library&amp;&amp; lib) noexcept;
      shared_library&amp; operator=(shared_library&amp;&amp; lib) noexcept;
      ~shared_library();

      explicit shared_library(const filesystem::path&amp; library_path);
      shared_library(const filesystem::path&amp; library_path, dl_mode mode);
      shared_library(const filesystem::path&amp; library_path, error_code&amp; ec);
      shared_library(const filesystem::path&amp; library_path, dl_mode mode, error_code&amp; ec);

      // ??.4.4, public member functions
      void reset() noexcept;
      explicit operator bool() const noexcept;

      template &lt;typename SymbolT&gt;
      SymbolT* get_if(const char* symbol_name) const;

      template &lt;typename SymbolT&gt;
      SymbolT* get_if(const string&amp; symbol_name) const;

      template &lt;typename SymbolT&gt;
      SymbolT&amp; get(const char* symbol_name) const;

      template &lt;typename SymbolT&gt;
      SymbolT&amp; get(const string&amp; symbol_name) const;

      native_handle_type native_handle() const noexcept;

      strong_ordering operator&lt;=&gt;(const shared_library&amp; other) const noexcept;

      // ??.4.5, public static member functions
      static constexpr bool platform_supports_dl() noexcept;
      static constexpr bool platform_supports_dl_of_program() noexcept;
    };

  }
  }
}
</pre>
<p><code>shared_library</code> defines member bitmask type <code>dl_mode</code>.</p>

<h4>??.4.1 Type dl_mode <span class="right">[dl.shared_library.dl_mode]</span></h4>
<pre>using dl_mode = <i>T1</i>;</pre>
<p>
    Bitmask type <code>dl_mode</code> provides modes for searching the shared library file in the file system [fs.general]
    and platform specific modes for loading the shared library file into the memory of program.
    <i>[ Note:</i> library users may extend available modes by
    casting the required platform specific mode to dl_mode: <i>[ Example:</i> <code>static_cast&lt;dl_mode&gt;(RTLD_NODELETE); </code><i> — end example ] — end note ]</i></p>

<h4>??.4.2 dl_mode constants <span class="right">[dl.shared_library.dl_const]</span></h4>
<pre>add_decorations</pre>
    <div class="desc"><i>Effects:</i> Adds a platform specific extensions and prefixes to shared library filename before trying to load it into program memory.
    If load attempts fail, loads with exactly specified name.</div>
    <div class="desc"><i>Value:</i> Any value that can not be received by applying binary OR to any set of modes from <code>dl_mode</code></div>
    <div class="desc"><i>[ Example:</i></div>
<pre>        // Attempts to open
        //      `./my_plugins/plugin1.dll` and `./my_plugins/libplugin1.dll` on Windows
        //      `./my_plugins/libplugin1.so` on Linux
        //      `./my_plugins/libplugin1.dylib` and `./my_plugins/libplugin1.so` on MacOS.
        // If that fails, loads `./my_plugins/plugin1`
        shared_library lib("./my_plugins/plugin1", dl_mode::add_decorations);
</pre>
<div class="desc"><i>- end example ]</i></div>

<div>
<pre>rtld_lazy</pre>
    <div class="desc"><i>Effects:</i> As if applying RTLD_LAZY on POSIX.</div>
    <div class="desc"><i>Value:</i> <code>0</code> if platform does not have RTLD_LAZY equivalent. Platform specific value of RTLD_LAZY equivalent otherwise.</div>

<pre>rtld_now</pre>
    <div class="desc"><i>Effects:</i> As if applying RTLD_NOW on POSIX.</div>
    <div class="desc"><i>Value:</i> <code>0</code> if platform does not have RTLD_NOW equivalent. Platform specific value of RTLD_NOW equivalent otherwise.</div>
<pre>rtld_global</pre>
    <div class="desc"><i>Effects:</i> As if applying RTLD_GLOBAL on POSIX.</div>
    <div class="desc"><i>Value:</i> <code>0</code> if platform does not have RTLD_GLOBAL equivalent. Platform specific value of RTLD_GLOBAL equivalent otherwise.</div>
<pre>rtld_local</pre>
    <div class="desc"><i>Effects:</i> As if applying RTLD_LOCAL on POSIX.</div>
    <div class="desc"><i>Value:</i> <code>0</code> if platform does not have RTLD_LOCAL equivalent. Platform specific value of RTLD_LOCAL equivalent otherwise.</div>

</div>

<pre>default_mode</pre>
    <div class="desc"><i>Value:</i> <code>rtld_lazy | rtld_local</code>.</div>

<pre>search_system_directories</pre>
    <div class="desc"><i>Value:</i> Any value that can not be received by applying binary OR to <code>dl_mode</code> constants from above.</div>
    <div class="desc"><i>Effects:</i> Allows loading of shared library files from system specific shared library file directories [fs.def.directory]
    along with loading shared library files from current directory.</div>

<h4>??.4.3 shared_library constructors <span class="right">[dl.shared_library.cons]</span></h4>
<pre>constexpr shared_library() noexcept;</pre>
    <div class="desc"><i>Effects:</i> Creates <code>shared_library</code> that does not reference any shared library file.</div>
    <div class="desc"><i>Ensures:</i> <code>!*this</code>.</div>

<pre>shared_library(shared_library&amp;&amp; lib) noexcept;</pre>
    <div class="desc"><i>Effects:</i> Assigns the state of <code>lib</code> to <code>*this</code> and sets <code>lib</code> to a default constructed state.</div>
    <div class="desc"><i>Remarks:</i> Does not invalidate symbols previously obtained from <code>lib</code>.</div>
    <div class="desc"><i>Ensures:</i> <code>!lib</code> and <code>!!*this</code>.</div>

<pre>shared_library&amp; operator=(shared_library&amp;&amp; lib) noexcept;</pre>
    <div class="desc"><i>Effects:</i> If <code>*this</code> then calls
 <code>reset()</code>. Assigns the state of <code>lib</code> to <code>*this</code> and sets <code>lib</code> 
to a default constructed state.</div>
    <div class="desc"><i>Remarks:</i> Does not invalidate symbols previously obtained from <code>lib</code>.</div>
    <div class="desc"><i>Ensures:</i> <code>!lib</code> and <code>!!*this</code>.</div>
    <div class="desc"><i>Returns:</i> <code>*this</code></div>

<pre>~shared_library();</pre>
<div class="desc">
    <i>Effects:</i> Destroys the <code>shared_library</code> by calling <code>reset()</code>.
</div>


<pre>explicit shared_library(const filesystem::path&amp; library_path);
shared_library(const filesystem::path&amp; library_path, dl_mode mode);
shared_library(const filesystem::path&amp; library_path, error_code&amp; ec);
shared_library(const filesystem::path&amp; library_path, dl_mode mode, error_code&amp; ec);</pre>
    <div class="desc"><i>Effects:</i> If the shared library file specified by <code>library_path</code> is already loaded in memory of current program
    makes <code>*this</code> reference the previously loaded shared library file.
    Otherwise loads a shared library file by specified path with a specified mode into the memory of current program,
    executes all the platform specific initializations.</div>

    <div class="desc">References current program if
    absolute or relative path to the program was provided as
    <code>library_path</code> and <code>platform_supports_dl_of_program()</code> is <code>true</code>.</div>
    <div class="desc">Loads shared library file with changed name and applied extension only if <code>(mode &amp; 
    dl_mode::add_decorations)</code> is not 0.
    Loads a shared library file from system specific shared library file
    directories only if <code>library_path</code> contains only shared library filename and
    <code>(mode &amp; dl_mode::search_system_directories)</code> is not 0. During loads
    from system
    specific directories file name modification rules from above apply. If
    with <code>dl_mode::search_system_directories</code> more than one
    shared library file has the same base name and extension, the function
    loads in memory any first matching shared library file.
    If <code>(mode &amp; dl_mode::search_system_directories)</code> is 0, then any 
    relative path or path that contains only filename is treated as a
    path in current working directory.</div>

    <div class="desc">Library open mode is equal to <code>(mode &amp; 
    ~dl_mode::search_system_directories &amp; ~dl_mode::add_decorations)</code>
    converted to a platform specific
    type representing shared library file load modes and adjusted to satisfy the 
    <code>dl_mode::search_system_directories</code> requirements from above. If mode is
    invalid for current platform, attempts to adjust the mode by 
    applying <code>dl_mode::rtld_lazy</code>
    and reports error if resulting mode
    is invalid. <i>[ Example:</i> If mode on POSIX was set to <code>rtld_local</code>, then it will be adjusted
    to <code>rtld_lazy | rtld_local</code>. <i>- end example ]</i></div>

    <div class="desc"><i>Throws:</i> As specified in [dl.errors]</div>

<h4>??.4.4 shared_library members <span class="right">[dl.shared_library.member]</span></h4>
<pre>void reset() noexcept;</pre>
    <div class="desc"><i>Effects:</i> Sets <code>*this</code> to a default constructed state. If <code>*this</code> was the last instance of <code>shared_library</code> referencing the shared library file,
    then all the resources associated with shared library file may be released. <i>[ Note:</i> Platform specific calls not always release all the resources associated with shared library file. For example, <code>dlclose</code> from POSIX "is not required to remove structures from an address space, neither is an implementation prohibited from doing so." <i>— end note ]</i></div>
    <div class="desc"><i>Remarks:</i> Symbols obtained from shared library file remain valid if there is at least one instance of <code>shared_library</code> referencing the shared library file.</div>
    <div class="desc"><i>Ensures:</i> <code>!*this</code>.</div>

<pre>explicit operator bool() const noexcept;</pre>
    <div class="desc"><i>Returns:</i> <code>true</code> if <code>*this</code> references a shared library file, <code>false</code> otherwise.</div>

<pre>template &lt;typename SymbolT&gt;
  SymbolT* get_if(const char* symbol_name) const;

template &lt;typename SymbolT&gt;
  SymbolT* get_if(const string&amp; symbol_name) const;</pre>
    <div class="desc"><i>Returns:</i> Pointer to symbol from shared library file that has the symbol name <code>symbol_name</code>, <code>nullptr</code> otherwise.</div>
    <div class="desc"><i>Remarks:</i> It's the user responsibility to 
    provide valid <code>SymbolT</code> type for symbol. Implementations may 
    provide additional checks for
    matching <code>SymbolT</code> type and actual symbol type. <i>[ Note:</i> For
    example implementations
    may check that <code>SymbolT</code> is a function pointer and that
    symbol with <code>symbol_name</code> allows execution. <i>— end note ]</i></div>

<pre>template &lt;typename SymbolT&gt;
  SymbolT&amp; get(const char* symbol_name) const;

template &lt;typename SymbolT&gt;
  SymbolT&amp; get(const string&amp; symbol_name) const;</pre>
    <div class="desc"><i>Returns:</i> <code>*get_if&lt;SymbolT&gt;(symbol_name)</code>.</div>
    <div class="desc"><i>Throws:</i> <code>system_error</code> if symbol with symbol name <code>symbol_name</code> does not exist or if the shared library file was not loaded.</div>

<pre>native_handle_type native_handle() const noexcept;</pre>
    <div class="desc"><i>Returns:</i> Native handler of the loaded in memory shared library file or default constructed native_handle_type if <code>*this</code> does not reference a shared library file.
    <i>[ Note:</i> This member allow implementations to provide access to implementation details. Actual use of these members is inherently non-portable. <i>— end note ]</i>
</div>

<pre>strong_ordering operator&lt;=&gt;(const shared_library&amp; other) const noexcept;</pre>
    <div class="desc"><i>Returns:</i> result of implementation specific comparison of <code>this-&gt;native_handle()</code> and <code>other.native_handle()</code></div>


<h4>??.4.5 shared_library static members <span class="right">[dl.shared_library.static]</span></h4>

<pre>static constexpr bool platform_supports_dl() noexcept;</pre>
    <div class="desc"><i>Returns:</i> <code>true</code> if platform supports loading of shared library files into the memory of program, <code>false</code> otherwise.
    [ Note: If this function returns <code>false</code>, then any attempt to load a shared library file will fail at runtime. — end note ]</div>


<pre>static constexpr bool platform_supports_dl_of_program() noexcept</pre>
    <div class="desc"><i>Returns:</i> <code>true</code> if according to platform capabilities <code>shared_library(program_location())</code> may succeed, <code>false</code> otherwise.</div>



<h4>??.5 Runtime path functions <span class="right">[dl.location]</span></h4>
<pre>template&lt;class T&gt;
filesystem::path symbol_location(const T&amp; symbol, error_code&amp; ec);
template&lt;class T&gt;
filesystem::path symbol_location(const T&amp; symbol);</pre>
    <div class="desc"><i>Returns:</i> Full path and name to shared library file or program that contains <code>symbol</code></div>
    <div class="desc"><i>Throws:</i> As specified in [dl.errors]</div>

<pre>filesystem::path this_line_location(error_code&amp; ec);
filesystem::path this_line_location();</pre>
    <div class="desc"><i>Returns:</i> Full path and name to shared library file or program that contains line of code in which <code>this_line_location()</code> was called</div>
    <div class="desc"><i>Throws:</i> As specified in [dl.errors]</div>

<pre>filesystem::path program_location(error_code&amp; ec);
filesystem::path program_location();</pre>
    <div class="desc"><i>Returns:</i> Full path and name to the current program.</div>
    <div class="desc"><i>Throws:</i> As specified in [dl.errors]</div>

<h4>??.5 shared_library hash support <span class="right">[dl.shared_library.hash]</span></h4>
<pre>template &lt;&gt; struct hash&lt;experimental::shared_library&gt;;</pre><p>
    <div class="desc">
        The specialization is enabled.
    </div>

		<h3>Feature-testing macro</h3>
    <p>Add a row into the "Standard library feature-test macros" table [support.limits.general]:</p>
    <table border="1"><tr><td><code>__cpp_lib_dl</code></td><td>201811</td><td><code>&lt;experimental/dl&gt;</code></td></tr></table>


		<h2>V. Revision History</h2>
		<p>Revision 4:</p>
		<ul>
			<li>Default shared_library constructor is now <code>constexpr</code></li>
			<li>Use element names from P0788R3</li>
			<li>Do not require <code>reset()</code> to release all the resources associated with shared library file</li>
			<li>Cleanup the wording, use spaceship operator</li>
			<li>Rebased to N4762</li>
		</ul>
		<p>Revision 3:</p>
		<ul>
			<li>Changed Dynamic Library Load(DLL) term to Dynamic Loading (DL)</li>
			<li>Embedded EWG voting results</li>
			<li>Dropped copy constructors, assignment operators and assign functions of shared_library</li>
			<li>Dropped shared_library::location functions</li>
		</ul>
		<p>Revision 2:</p>
		<ul>
			<li>Added EWG questions</li>
			<li>Added motivation for not using <code>string_view</code></li>
			<li>Return pointer from get</li>
			<li>Some usage numbers and examples</li>
			<li>Specified and reduced load modes</li>
			<li>Fixed "must" usage in proposal</li>
			<li>Dropped <code>load()</code> functions</li>
			<li>Revised constructor overloads</li>
			<li>Dropped <code>import</code> functions</li>
			<li>Added feature testing macro</li>
			<li>"append_decorations" => "add_decorations"</li>
			<li><code>dl_mode</code> description rewritten</li>
			<li>Multiple minor improvements</li>
			<li>Rebased to N4687</li>
		</ul>
		<p>Revision 1:</p>
		<ul>
			<li>
				Rebased to N4618 and applied minor fixes
			</li>
		</ul>
		<p>Revision 0:</p>
		<ul>
			<li>
				Initial proposal
			</li>
		</ul>


		<h2>VI. Acknowledgements</h2>
		<p>Klemens Morgenstern highlighted some of the missing functionality 
        in Boost.DLL and provided implementation of mangled symbols load,
        that showed complexities of such approach. Renato Tegon Forti started the work on Boost.DLL and
        provided a lot of code, help and documentation for the Boost.DLL.</p>
        <p>Thanks to Tom Honermann for providing comments and recommendations.</p>
        <p>Thanks to Vasily Kulikov for pointing out that dlclose does not necessarily releases the resources associated with shared library file.</p>

		<h2>VII. References</h2>
		<p>[<a name="Boost.DLL">Boost.DLL</a>] Boost DLL library.
			Available online at <a href="https://github.com/boostorg/dll">https://github.com/boostorg/dll</a></p>
		<p>[<a name="N4762">N4762</a>] Working Draft, Standard for Programming Language C++.
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4762.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4762.pdf</a></p>


		<p>[<a name="N1400">N1400</a>] Toward standardization of dynamic libraries.
			Available online at <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2002/n1400.html">http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2002/n1400.html</a></p>
		<p>[<a name="N1418">N1418</a>] Dynamic Libraries in C++.
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1418.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1418.html</a></p>
		<p>[<a name="N1428">N1428</a>] Draft Proposal for Dynamic Libraries in C++.
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1428.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1428.html</a></p>
		<p>[<a name="N1496">N1496</a>] Draft Proposal for Dynamic Libraries in C++ (Revision 1).
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1496.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1496.html</a></p>
		<p>[<a name="N1976">N1976</a>] Dynamic Shared Objects: Survey and Issues.
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1976.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1976.html</a></p>
		<p>[<a name="N2015">N2015</a>] Plugins in C++.
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2015.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2015.pdf</a></p>
		<p>[<a name="N2407">N2407</a>] C++ Dynamic Library Support.
			Available online at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2407.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2407.html</a></p>
		<p>&nbsp;</p>



        <script type="text/javascript">
            function colorize_texts(texts) {
                for (var i = 0; i < texts.length; ++i) {
                    var text = texts[i].innerHTML;
                    text = text.replace(/namespace|enum|void|constexpr|extern|noexcept|bool|template|class |struct|auto|const|typename|explicit|public|private|operator|#include|char|typedef|static_assert|static_cast|static/g,"<span class='cppkeyword'>$&<\/span>");
                    text = text.replace(/\/\/[\s\S]+?\n/g,"<span class='cppcomment'>$&<\/span>");
                    //text = text.replace(/\"[\s\S]+?\"/g,"<span class='cpptext'>$&<\/span>");
                    texts[i].innerHTML = text;
                }
            }

            colorize_texts(document.getElementsByTagName("pre"));
            colorize_texts(document.getElementsByTagName("code"));

            function show_hide_deleted() {
                var to_change = document.getElementsByClassName('changed-deleted');
                for (var i = 0; i < to_change.length; ++i) {
                    to_change[i].style.display = (document.getElementById("show_deletions").checked ? 'inline' : 'none');
                }
            }
        </script>


</body></html>
