Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are symbols from the C standard library reserved in C++?

This is a follow-up to a different question.

The original question had other problems but I had to realize that the main one (according to CLang) was a redefinition of time as a different symbol while only nice C++ includes were used.

So here is a stripped-down version:

#include<iostream>

using std::cout;
using std::endl;

class time
{
public:
    int h, min, sec;
};

const int full = 60;

void canonify(time& pre)     // Error here (line 14)
{
    pre.min += pre.sec / full;
    pre.h += pre.min / full;
    pre.sec %= full;
    pre.min %= full;
}
int main()
{
    time a;                  // and here (line 23)
    a.h = 3;
    a.min = 128;
    a.sec = 70;
    canonify(a);
    cout << a.h << ":" << a.min << ":" << a.sec << endl;
}

Of course, replacing time with a different symbol or using struct time is enough to get rid of the problem. Said differently my question is not how to make the code run, but only whether we have to see symbols from the C library as reserved tokens in C++. Clang 11 (on MSVC19) chokes with:

1>ess.cpp(14,15): error : must use 'class' tag to refer to type 'time' in this scope
1>...\ucrt\time.h(518,42): message : class 'time' is hidden by a non-type declaration of 'time' here
1>ess.cpp(23,5): error : must use 'class' tag to refer to type 'time' in this scope
1>...\ucrt\time.h(518,42): message : class 'time' is hidden by a non-type declaration of 'time' here

So the question is: where does the C++ standard forbid freely using symbols from the C standard library when they are not explicitly included in a compilation unit?


Interestingly enough the same code (once translated...) works fine in C:

#include <stdio.h>

//
typedef struct 
{
    int h, min, sec;
}time;
//
const int full = 60;
//
void canonify(time* pre)
{
    pre->min += pre->sec / full;
    pre->h += pre->min / full;
    pre->sec %= full;
    pre->min %= full;
}
int main()
{
    time a;
    a.h = 3;
    a.min = 128;
    a.sec = 70;
    canonify(&a);
    printf("%d:%d:%d\n", a.h, a.min, a.sec);
    return 0;
}
like image 624
Serge Ballesta Avatar asked Jul 20 '21 12:07

Serge Ballesta


People also ask

What is standard library function in C?

C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.

What is the namespace of the C standard library?

All C library names are reserved in the global namespace in C++. [extern. names]/3: Each name from the Standard C library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.

Why is C plus plus standard library required?

The C++ Standard Library provides several generic containers, functions to use and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and functions for everyday tasks such as finding the square root of a number.

What is the C standard library?

] The C standard library or libc is the standard library for the C programming language, as specified in the ANSI C standard. It was developed at the same time as the C library POSIX specification, which is a superset of it.

How is C library different from other languages?

Comparison to standard libraries of other languages The C standard library is small compared to the standard libraries of some other languages. The C library provides a basic set of mathematical functions, string manipulation, type conversions, and file and console-based I/O.

What is ANSI C library?

In 1983 the American National Standards Institute (ANSI) formed a committee to establish a standard specification of C known as "ANSI C". This work culminated in the creation of the so-called C89 standard in 1989. Part of the resulting standard was a set of software libraries called the ANSI C standard library.

What is libc in C language?

Miscellaneous headers. The C standard library or libc is the standard library for the C programming language, as specified in the ANSI C standard. It was developed at the same time as the C library POSIX specification, which is a superset of it.


2 Answers

[extern.names]

3 Each name from the C standard library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.

Note that this paragraph reserves the name itself. So aliasing time in the global namespace violates this contract.

like image 145
StoryTeller - Unslander Monica Avatar answered Oct 19 '22 07:10

StoryTeller - Unslander Monica


Where does the C++ standard forbids to use freely symbols from the C standard library

Latest draft:

[extern.names]

Each name from the C standard library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.

Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both extern "C" and extern "C++" linkage, or as a name of namespace scope in the global namespace.


when they are not explicitely included in a compilation unit?

If you use the standard library at all, then all name reservations of the standard library are in effect.

If you include any standard header (or, any header whose exact content you don't control and thus may include standard headers), then you may be indirectly including other standard headers, including those inherited from C.

like image 26
eerorika Avatar answered Oct 19 '22 05:10

eerorika