Is a standard library required for standard compliance?

Is a standard library of standard compliance required? I have a feeling that the standard library is not standard. The basis of this feeling is the error messages generated by the compiler (s). For example, once GCC gives error messages that start with the __gxx prefix and many others that I don’t remember at the moment. But, seeing them, I feel that these are very compiler-specific messages, and different compilers will not be able to compile the standard library provided by GCC, and vice versa. It's true?

The question can be asked in other words like:

  • Can the standard library provided by one compiler compile with other compilers?
  • When we say that a particular compiler is standard, does it automatically mean that the stdlib that comes with it is also standard compatible? Or does it just mean that this compiler can compile standard compatible code written by us programmers?
  • Is it possible to use the standard library provided by one compiler in my project, which uses the compiler to compile the project? Is portability the same as standard compatibility?

These questions have different angles to look at the same big question. So, please help me understand what this means when we say that the X compiler is standard compatible.

+4
source share
8 answers

Yes, standard libraries should follow the standard, but there is a bit of flexibility. The standard does not require a specific implementation of functions, and an implementation can freely add internal functions, attributes ... as long as the requirements are met.

Please note that there is another library concept that complies with the standard, and the library is implemented using only standard functions.

For specific questions below:

Can the standard library provided by one compiler compile with other compilers?

Some will, some will not. An implementation of the standard library can use the built-in compiler functions for some operations, functions that are present on only one platform, and not others ... Some STL implementations can be compiled with various compilers, although, for example, STLPort, Dinkumware (which is also VS, with some VS-modifications)

When we say that a particular compiler is standard, does it automatically mean that the stdlib that comes with it is also standard compatible? Or does it just mean that this compiler can compile standard compatible code written by us programmers?

This means that the library must be compatible, but again, the standard does not provide for the implementation of the library, and they can use non-standard extensions and the like, which will work in one compiler, but maybe not in other compilers. Consider, for example , the implementation of shared_ptr , the reference counter should be updated atomically, but in the current standard there were no atomic operations with integers, so it should be implemented in terms of non-standard functions.

Is it possible to use the standard library provided by one compiler in my project, which uses another compiler to compile the project? Is portability the same as standard compatibility?

Not necessary.

+4
source

The standard library is an implementation detail. It cannot even be “compiled” in the sense that the standard does not require it to consist of “files” [headers]:

174) The header is not necessarily the source file, just as the sequences limited by the <and> symbol in the header names are necessarily the names of the source files (16.2).

The standard thoroughly simplifies implementation requirements, so the library can be "embedded" in the compiler (aka intrinsics). For example, expanding the std or # determining the name used in the standard library gives you undefined behavior.

+5
source

Is a standard library of standard compliance required? I have a feeling that the standard library is not standard.

By definition, an implementation must conform to the standard of conformance standard, yes.

Otherwise, this is not an implementation of the standard C ++ library, but some other thing.

When we say that a particular compiler is standard, does it automatically mean that the stdlib that comes with it is also standard?

It will depend on the wording, right? If a program chain involves the introduction of a standard library, we can safely assume that it is compatible. The fact that the compiler executable itself is compatible is not the same thing if the library implementation is not embedded in the compiler executable.

But this is just a pun.

Is it possible to use the standard library provided by one compiler in my project, which uses another compiler to compile the project? Is portability the same as standard compatibility?

Of course not. Two compatible implementations may be completely incompatible with each other.

For example, once GCC gives error messages that start with the __gxx prefix and many others that I don’t remember at the moment.

It's good. A toolchain can implement a library, but it chooses if the interface conforms to the standard. The standard does not say that an implementation cannot use the __gxx symbol in its work.


BTW

Although, he says that a programmer cannot use characters with leading underscores, in some cases!

You are not allowed ( [lib.requirements] / [requirements] ) to use any name that:

  • contains two consecutive underscores,
  • begins with an underscore followed by an uppercase letter
  • starts with an underscore and is in the global namespace

Also, in C ++ 0x, letter suffixes are reserved that do not begin with underscores.

+3
source

The library user API must be standard; however, an implementation does not have to be. You see an example of this, where the standard version of the function is replaced by the optimized version (with a different name, because sometimes replacement is possible only in some cases, for example, when the corresponding alignment can be proved). This also means that the implementation of one standard compiler library may not be able to be created by another compiler (which may lead to boot problems during porting, but this is another problem).

+1
source

what does this exactly mean when we say that the X compiler is standard compatible

This means that the compiler provides the standard library with all the requirements set by the standard, and that the implementation itself meets all the requirements of the standard. The fact is that the "requirements" for the standard library are quite liberal after a common interface.

+1
source

The standard library must conform to the minimum interface. In it, an implementation can do whatever it wants, because, well, it is an implementation, although I am sure that there are some identifier restrictions and such to prevent collisions.

The implementation of the standard library does not have to be portable. They can be, but far from necessary.

0
source

The standard places restrictions on the interface of the standard library, not the implementation. He does his best to understand that standard library headers can do what the user code cannot execute ... __MACRO__NAME__ reserved for implementation, for example. And, obviously, only an implementation can actually put all of these functions and types into the std namespace.

Basically, a “portable” implementation can be written. But, most likely, it will be less productive ... as a simple example, consider the traditional implementation of the offsetof macro. This is usually due to dereferencing the null pointer, which is formally undefined behavior, but since the implementation knows how its platform works, everything is fine. The portable version cannot do this, so actually create a new instance of the provided type so that everything is on top and above.

A few traits of type in C ++ 0x probably require compiler support, which makes the "portable" implementation somewhere between difficult and impossible. There is no standard analysis method if, for example, an arbitrary POD type, therefore boost::is_pod requires user support through specialization on some platforms.

It is also believed that most of the standard library is not just for headers. Other bits can be written in a completely different language ... as long as everything is connected correctly, it does not matter. If the runtime is implemented in Lisp, it is obvious that it will not be C ++ compatible code that can be reliably dropped into another chain of compiler tools.

0
source

Can the standard library provided by one compiler compile with other Compilers?

Assuming the library is up to standard, yes (I know this is some kind of chicken-egg).

When we say that a particular compiler is standard, does it automatically mean that the stdlib that comes with it is also Standard compatible? Or just means that this compiler can compile standard compatible code written by us programmers?

Yes, although I do not know a single fully validating compiler. Please note that the standards that apply to us as programmers are different from the standard library. For example, identifiers containing __ (double underscore) are allowed for implementation.

Is it possible to use the standard library provided by one compiler in my project, which uses the compiler to compile the project? Is portability the same as standard compliance?

You should be able to compile and use such a library. You will almost certainly not be able to use compiled library files (static or dynamic) because the name change will be different.

0
source

All Articles