On Thursday, April 19, 2018 at 5:19:12 PM UTC-4,
supe...@casperkitty.com wrote:
> On Wednesday, April 18, 2018 at 8:21:05 PM UTC-5, Richard Damon wrote:
...
> > I have NEVER heard of the assumption that a C++ function is only
> > callable from a file compiled at the same time as it was. There are
> > plenty of C++ based libraries.
>
> Such libraries typically require that imported and exported functions
> be marked with an 'extern "C"' directive, do they not?
No, it's perfectly feasible to have libraries with functions that have
C++ language linkage.
> ... And aren't many
> C++ features and concepts limited to code that is not thus marked?
No. Language linkage affects only two things. One of those is function
types - the language linkage basically determines the calling convention
used to call the function and to obtain the value returned by that
function. The other is the relationship between the name used inside C++
code for functions and objects with external linkage, and the name given
to the linker when linking the entire program together. For example, with
C language linkage, the two names are generally very closely related -
the linker might see an "_" added at either the beginning or end of the
name. However, with C++ language linkage, the linker generally sees a
mangled name that uniquely identifies, among other things, the types of
the arguments of the function.
By clever use of typedefs, you can mix and match these things, creating,
for instance, a function whose name has "C" language linkage, and whose
type has "Pascal" language linkage.
This implies that function types with linkage for a particular language
cannot have an interface that uses C++ features with no comparable
feature in the specified language, so some limitations do apply.
Arguments and the return value cannot have types with no equivalent in
the specified language. Much of this implementation-defined, so in
principle a compiler could translate a function type with extern "C"
linkage that uses a C++ reference as if it actually used a pointer,
instead. However, inside the body of such a function, you can make use
of any feature of C++ you want (well, not quite - code which throws an
exception without catching it can be a problem for a function whose
type doesn't have C++ language linkage - but that's because throwing an
uncaught exception makes the exception effectively part of your calling
convention).