for (;;) {
//Something to be done repeatedly
}
Run Code Online (Sandbox Code Playgroud)
我已经看到这种东西使用了很多,但我觉得它很奇怪......说再说清楚while(true),还是沿着这些方向做些什么?
我猜这(因为许多程序员采用神秘的代码的原因)这是一个微小的利润更快?
为什么,它真的值得吗?如果是这样,为什么不这样定义它:
#define while(true) for(;;)
Run Code Online (Sandbox Code Playgroud)
回到我的C/C++时代,将"无限循环"编码为
while (true)
Run Code Online (Sandbox Code Playgroud)
感觉更自然,对我来说似乎更明显,而不是
for (;;)
Run Code Online (Sandbox Code Playgroud)
在1980年代后期与PC-lint的相遇以及随后的最佳实践讨论让我想起了这种习惯.我已经使用for控制语句对循环进行了编码.今天,很长一段时间以来,也许我第一次需要作为C#开发人员的无限循环,我面临同样的情况.其中一个是正确的而另一个不是吗?
我只想知道for(;;)以下代码中该行的原因.
//
// blocking_tcp_echo_server.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
using boost::asio::ip::tcp;
const int max_length = 1024;
typedef boost::shared_ptr<tcp::socket> socket_ptr;
void session(socket_ptr sock)
{
try
{
for (;;)
{
char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), …Run Code Online (Sandbox Code Playgroud)