永远不会终止有效 C++ 程序的程序是什么?
Is a program that never terminates a valid C++ program?
程序是否需要终止?换句话说,是一个在技术上永远运行的未定义行为的程序吗?请注意,这与空循环无关。谈论永远 "stuff"(即可观察到的行为)的程序。
例如像这样:
int main()
{
while (true)
{
try
{
get_input(); // calls IO
process();
put_output(); // calls IO, has observable behavior
// never break, exit, terminate, etc
} catch(...)
{
// ignore all exceptions
// don't (re)throw
// never go out of loop
}
}
}
这更像是一个学术问题,因为根据经验,所有理智的编译器都会为上述类型的程序生成预期的代码(当然假设没有其他 UB 来源)。是的,当然有很多程序永远不会终止(os、嵌入式、服务器)。然而,标准有时很古怪,因此是个问题。
切线:"algorithm" 的许多(某些?)定义要求算法必须终止,即一系列永不终止的操作不被视为算法。
切线。暂停问题指出不存在一种算法来确定任意程序是否完成输入。然而对于这个特定的程序,由于没有导致退出 main 的分支,编译器可以很容易地确定程序永远不会结束。然而,这无关紧要,因为问题是语言律师。
是的。来自 [intro.progress]
The implementation may assume that any thread will eventually do one
of the following:
- terminate,
- make a call to a library I/O function,
- perform an access through a volatile glvalue, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of
empty loops, even when termination cannot be proven. — end note ]
C++ 标准中没有任何要求程序或任何给定线程终止的内容。最接近的是 [intro.progress]p1,它表示
The implementation may assume that any thread will eventually do one of the following:
- terminate,
- make a call to a library I/O function,
- perform an access through a volatile glvalue, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]
只要有一些可观察到的行为,最终,或者只要它把所有时间都花在I/O操作或另一个阻塞库调用上,这不适用,并且程序有效(假设它满足所有其他有效性标准)。
程序是否需要终止?换句话说,是一个在技术上永远运行的未定义行为的程序吗?请注意,这与空循环无关。谈论永远 "stuff"(即可观察到的行为)的程序。
例如像这样:
int main()
{
while (true)
{
try
{
get_input(); // calls IO
process();
put_output(); // calls IO, has observable behavior
// never break, exit, terminate, etc
} catch(...)
{
// ignore all exceptions
// don't (re)throw
// never go out of loop
}
}
}
这更像是一个学术问题,因为根据经验,所有理智的编译器都会为上述类型的程序生成预期的代码(当然假设没有其他 UB 来源)。是的,当然有很多程序永远不会终止(os、嵌入式、服务器)。然而,标准有时很古怪,因此是个问题。
切线:"algorithm" 的许多(某些?)定义要求算法必须终止,即一系列永不终止的操作不被视为算法。
切线。暂停问题指出不存在一种算法来确定任意程序是否完成输入。然而对于这个特定的程序,由于没有导致退出 main 的分支,编译器可以很容易地确定程序永远不会结束。然而,这无关紧要,因为问题是语言律师。
是的。来自 [intro.progress]
The implementation may assume that any thread will eventually do one of the following:
- terminate,
- make a call to a library I/O function,
- perform an access through a volatile glvalue, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]
C++ 标准中没有任何要求程序或任何给定线程终止的内容。最接近的是 [intro.progress]p1,它表示
The implementation may assume that any thread will eventually do one of the following:
- terminate,
- make a call to a library I/O function,
- perform an access through a volatile glvalue, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]
只要有一些可观察到的行为,最终,或者只要它把所有时间都花在I/O操作或另一个阻塞库调用上,这不适用,并且程序有效(假设它满足所有其他有效性标准)。