Possible implementation in C++
#define NULL 0
//since C++11
#define NULL nullptr
Notes
In C, the macro NULL may have the type void*, but that is not allowed in C++.
#ifndef NULL
#ifdef __cplusplus
#define NULL 0 //c++中定义为0
#else
#define NULL ((void *)0) //c语言中定义为void*
#endif
#endif
NULL 问题
#include <cstddef>
#include <iostream>
void test(int i)
{
std::cout << "int i\n";
}
void test(int * i)
{
std::cout << "int * i\n";
}
int main()
{
test(0); //int i
test(NULL); //int i,在这里会被当做0来使用。
return 0;
}
#include <cstddef>
#include <iostream>
template<class F, class A>
void Fwd(F f, A a)
{
f(a);
}
void g(int * i)
{
std::cout << "Function g called\n";
}
int main()
{
g(NULL); //Function g called
g(0); //Function g called
Fwd(g, nullptr); //Function g called
//Fwd(g, NULL); //“void (int *)”: 无法将参数 1 从“int”转换为“int *”
}
#include <cstddef>
#include <iostream>
using namespace std;
void f(int* pi)
{
std::cout << "Pointer to integer overload\n";
}
void f(double* pd)
{
std::cout << "Pointer to double overload\n";
}
void f(std::nullptr_t nullp)
{
std::cout << "null pointer overload\n";
}
int main()
{
int* pi = NULL;
double* pd = NULL;
f(pi);
f(pd);
f(nullptr); // would be ambiguous without void f(nullptr_t)
// f(0); // ambiguous call: all three functions are candidates
// f(NULL); // ambiguous if NULL is an integral null pointer constant
// (as is the case in most implementations)
}
class nullptr_t
{
public:
template<class T>
inline operator T*() const //定义类型转换操作符,使nullptr_t 可转为任意非类成员指针类型
{ return 0; }
//重载类型转换操作符,使 nullptr_t 可以转换为类 C 中任意的指针类型(数据成员指针/函数成员指针)
//对类中数据成员的指针,T 将被推导为数据成员的类型 eg: int (X::*); 此时 T 为 int,C 为 X
//对类中函数成员的指针,T 将被推导为函数成员的类型 eg: int (X::*)(int); 此时T 等效于: typedef int (T)(int)
template<class C, class T>
inline operator T C::*() const
{ return 0; }
private:
void operator&() const;
};
const null_ptr nullptr = {}
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc, char * argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: client <host>" << std::endl;
return 1;
}
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], "daytime");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
for (;;)
{
boost::array<char, 128> buf;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buf), error);
if (error == boost::asio::error::eof)
{
break;
}
else if (error)
{
throw boost::system::system_error(error);
}
std::cout.write(buf.data(), len);
}
}
catch (std::exception e)
{
std::cerr << "catch error: " << e.what() << std::endl;
}
return 0;
}
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
int main()
{
try
{
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);
std::string message = make_daytime_string();
boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
}
}
catch (std::exception & e)
{
std::cerr << "catch error: " << e.what() << std::endl;
}
return 0;
}
#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service & io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket & socket()
{
return socket_;
}
void start()
{
message_ = make_daytime_string();
boost::asio::async_write(
socket_,
boost::asio::buffer(message_),
boost::bind(
&tcp_connection::handle_write,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
));
}
private:
tcp_connection(boost::asio::io_service & io_service)
: socket_(io_service)
{
}
void handle_write(const boost::system::error_code &, size_t)
{
}
tcp::socket socket_;
std::string message_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service & io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
{
start_accept();
}
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
acceptor_.async_accept(
new_connection->socket(),
boost::bind(
&tcp_server::handle_accept,
this,
new_connection,
boost::asio::placeholders::error
)
);
}
void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code & error)
{
if (!error)
{
new_connection->start();
}
start_accept();
}
tcp::acceptor acceptor_;
};
int main()
{
try
{
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception & e)
{
std::cerr << "catch error: " << e.what() << std::endl;
}
return 0;
}
项目右键点击–>属性–> C/C++ –> 常规 –> 附加包含目录
C:\local\boost_1_64_0
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " "
);
}
项目右键点击–>属性–> C/C++ –> 常规 –> 附加包含目录
C:\local\boost_1_64_0
项目右键点击–>属性–> VC++ 目录 –> 库目录
C:\local\boost_1_64_0\lib32-msvc-14.0
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service & io)
: strand_(io),
timer1_(io, boost::posix_time::seconds(1)),
timer2_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
~printer()
{
std::cout << "Final count is " << count_ << std::endl;
}
void print1()
{
if (count_ < 10)
{
std::cout << "Timer 1: " << count_ << std::endl;
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
}
}
void print2()
{
if (count_ < 10)
{
std::cout << "Timer 2: " << count_ << std::endl;
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
}
private:
boost::asio::io_service::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/tutorial/tuttimer5.html
项目右键点击–>属性–> C/C++ –> 常规 –> 附加包含目录
C:\local\boost_1_64_0
项目右键点击–>属性–> VC++ 目录 –> 库目录
C:\local\boost_1_64_0\lib32-msvc-14.0
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service & io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this));
}
~printer()
{
std::cout << "Final count is " << count_ << std::endl;
}
void print()
{
if (count_ < 5)
{
std::cout << count_ << std::endl;
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
return 0;
}
http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/tutorial/tuttimer4.html
项目右键点击–>属性–> C/C++ –> 常规 –> 附加包含目录
C:\local\boost_1_64_0
项目右键点击–>属性–> VC++ 目录 –> 库目录
C:\local\boost_1_64_0\lib32-msvc-14.0
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code & e,
boost::asio::deadline_timer * t,
int * count)
{
if (*count < 5)
{
std::cout << *count << std::endl;
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
t->async_wait(boost::bind(print,
boost::asio::placeholders::error, t, count));
}
}
int main()
{
boost::asio::io_service io;
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(boost::bind(print,
boost::asio::placeholders::error, &t, &count));
io.run();
std::cout << "Final count is " << count << std::endl;
return 0;
}
http://www.boost.org/doc/libs/1_64_0/doc/html/boost_asio/tutorial/tuttimer3.html