Take the 2-minute tour ×

Given this interface defined in one binary:

struct ISomething
    virtual void __stdcall DoSomething() = 0;

Is this function signature, defined in another binary, safe?

bool __stdcall RunSomething(const ISomething &something);

or, must it be

bool __stdcall RunSomething(const ISomething *something);

(Assume __stdcall is #defined to nothing on a non-Visual Studio compiler.)

share|improve this question
The question is a little unclear. I can have a binary declaration for foo() and a separate declaration for foo() in another binary and these two methods have no correlation. What exactly are you asking? –  RageD Dec 31 '12 at 17:30
@RageD For example, RunSomething could be exported from the main binary, and code within the shared object would call the function with a concrete implementation of ISomething. –  moswald Dec 31 '12 at 17:31
If this didn't work, it would (practically) mean you couldn't have reference parameters in standard functions... kind of restraining. AFAIK, the C++ standard doesn't define anything for DLLs (or similar), so any answer would be implementation defined. –  Mat Dec 31 '12 at 17:34
Oh - I was not aware that you were using a DLL (shared code), sorry about that. –  RageD Dec 31 '12 at 17:36
@Mat Right: the standard doesn't define an ABI. Therefore, everything must be passed around as a pointer (for instance, passing std::string is asking for trouble, just pass a const char *). However, since & is really just syntactic sugar for * under the hood, I wonder if it's safe to use in this case. –  moswald Dec 31 '12 at 17:38
show 3 more comments

1 Answer

up vote 0 down vote accepted

References should work fine between binaries. They're implemented at the assembly level as pointers. I think there are examples of this in the Standard C++ and Boost libraries (maybe std::string::string(const std::string&)).

You might run into trouble with name mangling, though, if the binaries weren't built with the same compiler and version, but that has nothing to do with references.

share|improve this answer
Actually std::string::string is std::basic_string<char>::basic_string<char>, a template function. Also if references are "implemented as pointers", does this mean references are implementation dependent and may not work with two binaries from two different compilers (even if the name mangling is the same)? –  Seth Carnegie Dec 31 '12 at 17:37
Thanks, that's what I was suspecting, but I didn't want to end up shooting myself in the foot. –  moswald Dec 31 '12 at 17:39
Re: name mangling, the function should be wrapped with extern "C" when defined. –  moswald Dec 31 '12 at 17:41
Yes, we could get into an argument over "what the C++ standard says" vs. "what every compiler in the past decade or two has implemented." In particular (taking Windows as an example), the OS and/or architecture imposes an ABI which determines the implementation of references and pointers. Evey single one I've seen treats them both the same (they're integer byte addresses) because the x86 (and ARM and others) architecture has no independent concept of a "reference". –  Mike DeSimone Dec 31 '12 at 17:42
Further, if different compilers did different things, their binaries couldn't interoperate at the pointer level, much less with references. –  Mike DeSimone Dec 31 '12 at 17:43
show 7 more comments

Your Answer


