4

Regarding using const function parameters I've heard that on some OS X systems the constness of a parameter is mangled into the function signature. For example, if one would have the following declaration in an interface header file:

int f(int argument);

but if one would only implement this function:

int f(int const argument);

then this might lead to a linking failures on OS X (but not on Linux) because the OS X way to mangle C++ function signatures includes the constness of the parameters.

Which is the correct mangling behaviour? Does the C++ standard have a say on this?

jotik
  • 17,044
  • 13
  • 58
  • 123
  • _`int f(int const argument);`_ is pretty redundant, but well if you insist that it's meaningful. – πάντα ῥεῖ Apr 10 '16 at 16:58
  • 3
    Out of interest, where did you hear this? This would be very broken, if true. – Oliver Charlesworth Apr 10 '16 at 16:58
  • The correct behavior is to ignore top-level `const`, which means `const` that applies to the parameter itself (as opposed to something to which the parameter refers). For example, `int *const` and `int *` are the same, but `int const *` and `int *` are different. Hard to believe OS X would have a compiler that gets this wrong though (it's a really basic part of C++ that's been well known since long before OS X existed). – Jerry Coffin Apr 10 '16 at 17:00
  • @OliverCharlesworth About 2 years ago some co-workers noticed some of the code not linking on OS X due to such mismatches, therefore I've tried to avoid such ever since. I don't remember exactly what toolchain they used. – jotik Apr 10 '16 at 17:03
  • 1
    @jotik That sounds unlikely. Can you reproduce it? – juanchopanza Apr 10 '16 at 17:03
  • @juanchopanza Sorry I don't have any OS X systems nearby. – jotik Apr 10 '16 at 17:15
  • Cannot reproduce. Just tested this with XCode 7.3 (Clang & G++/GCC), OSX El Capitan 10.11.4. ideone.com/kQzNgm and ideone.com/IJR3vu Links fine (dynamically and statically both tested). Doesn't matter whether or not I make the parameter const. The above code is what I used for testing dynamic linking. Also tested with pointers (dynamically). Same results. – Brandon Apr 10 '16 at 17:27

1 Answers1

4

The top-level const and volatile qualifiers should be ignored for the purposes of name mangling. This can be determined from two things in the standard. The first, is that a function's signature is used for name mangling.

In the C++14 standard, section 1.3.17 defines a signature:

name, parameter type list (8.3.5), and enclosing namespace (if any) [ Note: Signatures are used as a basis for name mangling and linking. — end note ]

To get the definition of a parameter type list, we then refer to section 8.3.5/5:

The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively. After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type. The resulting list of transformed parameter types and the presence or absence of the ellipsis or a function parameter pack is the function’s parameter-type-list. [ Note: This transformation does not affect the types of the parameters. For example, int()(const int p, decltype(p)) and int()(int, const int) are identical types. — end note ]

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132