Am I mistaken?
I understand that extern "C" prevents name mangling... but C++ doesn't just mangle names for the hell of it, it does it because it has to in order to allow for things like function overloading.
For example a C++ program can have any number of functions named "foo":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class A
{
void foo();
};
class B
{
void foo();
};
namespace n
{
void foo();
}
void foo();
void foo(int);
void foo(std::string);
// ... etc
|
These are all distinctly different functions and therefore must all be exported under different names. They obviously cannot all be exported as "foo". As a result their names have to be mangled to retain scope/type information.
And how would you export something like an overloaded operator without mangling?
|
std::ostream& operator << (std::ostream&, const SomeType&);
|
The whole point of preventing name mangling (I thought) was to have
C++ compiled code able to link to
C libraries (hence the name 'extern
"C"' and not 'extern "nomangle"')
If you're not going to put C code in the extern C block... then what's the point?
goocreations wrote: |
---|
How would you otherwise create an object in the library that is dynamically loaded during run-time? |
You'd do it the same way you're doing it now... you'd just remove the
extern "C"
part. C++ names can still be externally linked, their names will just be mangled.
L B wrote: |
---|
C++ code is used in extern "C" all the time. |
Can you give me an example of a library that does this? I have never seen it.