R
Ralf Goertz
Hi,
I am writing a library which will be used via dlopen and friends. I
noticed that I can use
extern "C" { //to avoid name mangling
void foo(std::string &s) {
std::cout<<s<<std::endl;
s="bar";
}
}
in the library and
typedef void (*foo_t)(std::string);
foo_t foo = dlysm(handle,"foo");
in the calling program. Of course if I do so, calling foo(s) in the
calling program doesn't change s. But I had expected a segfault because
the signatures aren't the same. I assume this works because with my
compiler (g++) foo(std::string) and foo(std::string&) are essentially
the same but if the nonref'd form is used the parameter will be copied
and the function will be called on with the address of the copy instead
of the original.
My questions are: Can I rely on this? I might want foo to sometimes
change the parameter and sometimes not. On the other hand, dlopen and Co
are C functions. Am I allowed to use references at all or am I just
lucky that it works?
Thanks,
Ralf
I am writing a library which will be used via dlopen and friends. I
noticed that I can use
extern "C" { //to avoid name mangling
void foo(std::string &s) {
std::cout<<s<<std::endl;
s="bar";
}
}
in the library and
typedef void (*foo_t)(std::string);
foo_t foo = dlysm(handle,"foo");
in the calling program. Of course if I do so, calling foo(s) in the
calling program doesn't change s. But I had expected a segfault because
the signatures aren't the same. I assume this works because with my
compiler (g++) foo(std::string) and foo(std::string&) are essentially
the same but if the nonref'd form is used the parameter will be copied
and the function will be called on with the address of the copy instead
of the original.
My questions are: Can I rely on this? I might want foo to sometimes
change the parameter and sometimes not. On the other hand, dlopen and Co
are C functions. Am I allowed to use references at all or am I just
lucky that it works?
Thanks,
Ralf