Is it safe for me to write: and skip the const_cast'ing?
No.
If it is not safe, please explain why.
-- From language side:
After reading the dcl.link I think exactly how the interoperability works between C and C++ is not exactly specified, with many "no diagnostic required" cases. The most important part is:
Two declarations for a function with C language linkage with the same function name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same function.
Because they refer to the same function, I believe a sane assumption would be that the declaration of a identifier with C language linkage on C++ side has to be compatible with the declaration of that symbol on C side. In C++ there is no concept of "compatible types", in C++ two declarations have to be identical (after transformations), making the restriction actually more strict.
From C++ side, we read c++draft basic#link-11:
After all adjustments of types (during which typedefs are replaced by their definitions), the types specified by all declarations referring to a given variable or function shall be identical, [...]
Because the declaration int foo(const char *str)
with C language linkage in a C++ translation unit is not identical to the declaration int foo(char *str)
declared in C translation unit (thus it has C language linkage), the behavior is undefined (with famous "no diagnostic required").
From C side (I think this is not even needed - the C++ side is enough to make the program have undefined behavior. anyway), the most important part would be C99 6.7.5.3p15:
For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types [...]
Because from C99 6.7.5.1p2:
For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types.
and C99 6.7.3p9:
For two qualified types to be compatible, both shall have the identically qualified version of a compatible type [...]
So because char
is not compatible with const char
, thus const char *
is not compatible with char *
, thus int foo(const char *)
is not compatible with int foo(char*)
. Calling such a function (C99 6.5.2.2p9) would be undefined behavior (you may see also C99 J.2)
-- From practical side:
I do not believe will be able to find a compiler+architecture combination where one translation unit sees int foo(const char *)
and the other translation unit defines a function int foo(char *) { /* some stuff */ }
and it would "not work".
Theoretically, an insane implementation may use a different register to pass a const char*
argument and a different one to pass a char*
argument, which I hope would be well documented in that insane architecture ABI and compiler. If that's so, wrong registers will be used for parameters, it will "not work".
Still, using a simple wrapper costs nothing:
static inline int foo2(const char *var) {
return foo(static_cast<char*>(var));
}
fooWrapper
function that does theconst_cast
for you. – Grattconst char*
argument and different register to pass achar *
argument to function. – Shoalfoo
I guess? – Lakishaint foo(const char*);
andint foo(char*)
are not compatible function declarations. – Shoalva_list
version of your vararg function? You could create wrapper for that without variadic templates. – Missconst char *s = "123"; foo((char *) s);
is OK whenfoo()
does not write tos[]
. Yet C++ and C are different. – Obliquelytemplate <typename T1> void bar(T1)
,template <typename T1, typename T2> void bar(T1, T2)
, ... – Eloisaeloiseextern "C"
so no need to worry about overloads... – Balmoralvoid foo(char* str, size_t len);
the declarationextern "C" int foo(const char* str);
will still make it possible to link it (by the linkers I know of). – Lakisha