Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all functions in the c++ standard library required have external linkage?

So I've got an app which compiles fine on windows, linux and a few variations of unix. I recently decided to port it to OSX when I ran into a snag.

I have a template which looks like this:

template<int (&F)(int)>
int safe_ctype(unsigned char c) { return F(c); }

the idea being to prevent sign extension from crashing certain implementations when given input values above 0x7f. It is typically used like this:

safe_ctype<std::isspace>(ch);

This unfortunately doesn't work on OSX (using gcc 4.2). The error has to do with std::isspace not having external linkage and therefore not applicable for templates. It turns out that on OSX, the ctype.h header has all functions (through macros) marked static inline.

Here's my question:

Is it permitted by any relevant standard for functions in the C++ (in this case the parts inherited from C's) standard library to not have external linkage?

EDIT:

I've heard back from apple. Apparently they have a macro to control this behavior. Defining _DONT_USE_CTYPE_INLINE_ prevents the ctype functions from being static inline.

like image 431
Evan Teran Avatar asked Aug 26 '10 03:08

Evan Teran


2 Answers

C++03 §17.4.2.2/1 says:

Entities in the C++ Standard Library have external linkage.

The same is true in C: C99 §7.1.2/6 says:

Any declaration of a library function shall have external linkage.

like image 163
James McNellis Avatar answered Oct 01 '22 20:10

James McNellis


The OS X <ctype.h> header protects the non-standard inline versions with a check that you are not compiling in standards mode.

If you don't tell the compiler you want conformance, you don't get conformance. This is true on almost all platforms, though in different ways.

If you want all the niceties of extensions and what not, and so don't want to require strict conformance, you can define _DONT_USE_CTYPE_INLINE_ before including the header and you will get non-inline versions of the functions with external linkage.

like image 44
Stephen Canon Avatar answered Oct 01 '22 18:10

Stephen Canon