Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between undefined behavior and "program is ill-formed"?

48 views
Skip to first unread message

Juha Nieminen

unread,
Nov 12, 2021, 8:54:32 AM11/12/21
to
With std::vector<T, Allocator>, if Allocator::value_type is not the same
as T, the behavior is undefined until C++20. Since C++20 the "program is
ill-formed".

What is the difference?

Alf P. Steinbach

unread,
Nov 12, 2021, 9:06:38 AM11/12/21
to
The in-practice:

UB can compile, with or without a diagnostic. Some/most kinds of UB are
dynamic events during program execution, such as integer division by 0,
dereferencing a nullpointer outside a `typeid` expressions, etc. It
would be impossible for a compiler to diagnose them all.

Ill formed program doesn't compile, and always causes a diagnostic.

The formal:

"Undefined Behavior" is (C++17 3.27) "behavior for which this
International Standard imposes no requirements".

"Ill formed" is just "not well formed", where "well formed" is just a
(C++17 §3.29) "program constructed according to the syntax rules,
diagnosable semantic rules, and the one-definition rule".

- Alf

Juha Nieminen

unread,
Nov 12, 2021, 9:11:07 AM11/12/21
to
So that means that starting from C++20 we are guaranteed to get a
compiler error if Allocator::value_type is not the same as T?

Bo Persson

unread,
Nov 12, 2021, 10:35:43 AM11/12/21
to
Yes. Easily implemented with a static_assert, which wasn't available
when vector was born, but is now.

James Kuyper

unread,
Nov 12, 2021, 11:35:31 AM11/12/21
to
When the behavior is undefined, the standard imposes no requirements
(3.30). Code which has undefined behavior is not considered to have
violated a diagnosable rule (4.1p1).
When a program is ill-formed, that qualifies as a violation of a
diagnosable rule, and as such, the standard imposes one requirement:
that at least one diagnostic message be generated.(4.1p2).

Tim Rentsch

unread,
Nov 13, 2021, 1:27:15 PM11/13/21
to
"Alf P. Steinbach" <alf.p.s...@gmail.com> writes:

[...]

> Ill formed program doesn't compile, and always causes a diagnostic.

The C++ standard identifies several sets of circumstances that
cause a program to be ill formed, but explicitly and specifically
do not require a diagnostic.

Tim Rentsch

unread,
Nov 13, 2021, 1:28:15 PM11/13/21
to

David Brown

unread,
Nov 13, 2021, 3:36:43 PM11/13/21
to
I think that breaking the "One definition rule" would count here. Are
there any others that you are thinking about?

Alf P. Steinbach

unread,
Nov 14, 2021, 9:43:18 AM11/14/21
to
Oh you're right, thanks. Sorry.

I was talking about the in-practice but clearly some ill formed programs
can't even practically be diagnosed as such.

The difference between UB and ill-formed gets less clear then, but
mainly that UB is well formed code that isn't constrained to any
behavior, while ill formed code -- is code that isn't well formed and
that isn't constrained to any behavior. A bit too circular for me. But I
guess syntactically invalid code must be ill-formed and cannot have UB.

- Alf

Paavo Helde

unread,
Nov 15, 2021, 7:16:08 AM11/15/21
to
"UB" is behavior which is undefined by the C++ standard. Another
standard or the implementation can define the behavior, making the
program valid.

I suspect "ill-formed" is meant for programs which cannot or should not
be made valid by another standard or implementation.




Tim Rentsch

unread,
Nov 15, 2021, 11:46:17 AM11/15/21
to
"Alf P. Steinbach" <alf.p.s...@gmail.com> writes:

> On 13 Nov 2021 19:26, Tim Rentsch wrote:
>
>> "Alf P. Steinbach" <alf.p.s...@gmail.com> writes:
>>
>> [...]
>>
>>> Ill formed program doesn't compile, and always causes a diagnostic.
>>
>> The C++ standard identifies several sets of circumstances that
>> cause a program to be ill formed, but explicitly and specifically
>> do not require a diagnostic.
>
> Oh you're right, thanks. Sorry.

No worries. Your description is mostly right, and in fact I
think it should _always_ be right. That there are exceptions
is IMO a flaw in how the C++ standard is written.

> I was talking about the in-practice but clearly some ill formed
> programs can't even practically be diagnosed as such.
>
> The difference between UB and ill-formed gets less clear then, but
> mainly that UB is well formed code that isn't constrained to any
> behavior, while ill formed code -- is code that isn't well formed
> and that isn't constrained to any behavior. A bit too circular
> for me. But I guess syntactically invalid code must be ill-formed
> and cannot have UB.

To be fair, any C++ construction that is ill-formed does require
a diagnostic, unless there is a specific statement that no
diagnostic is required. (Disclaimer: I am not 100% sure that
the previous sentence holds true in the case of violations of
the One Definition Rule.)

Tim Rentsch

unread,
Nov 15, 2021, 11:56:10 AM11/15/21
to
AFAICT any program construction that makes a program ill-formed
is the same as undefined behavior, except that the ill-formed
cases require a diagnostic (unless the C++ standard explicitly
says otherwise for the specific case in question).

Consider for example paragraph 8 in section 4.1:

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any well-formed program. Implementations are
required to diagnose programs that use such extensions that
are ill-formed according to this document. Having done so,
however, they can compile and execute such programs.
0 new messages