C++ templateテクニック

TLでC++ templateテクニックのGoogleネタが流行っていたのでそれなりに即興で(勉強して(ぇ)boost::asioでひたすら"もしかして"を検索するコードを書いてみました

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/optional.hpp>

boost::optional< std::string > my_search( const std::string & word )
{
	using namespace std;
	using namespace boost::asio;
	// www.google.co.jp の http サービスに接続
	ip::tcp::iostream s( "www.google.co.jp", "http" );

	// 送信
	s << "GET /" << "/search?" << word <<" HTTP/1.0\r\n";
	s << "Host: www.google.co.jp\r\n";
	s << "\r\n";
	s << flush;  // バッファに溜めずに確実にネットワーク送信

	// 受信
	string line = "";
	while( getline(s, line) )
	{
		const auto result_spell = line.find( "spell" );
		if( result_spell != string::npos )
		{
			const string line2 = line.substr( result_spell );
			const auto result_q = line2.find( "q=" );
			if( result_q != string::npos )
			{
				const string if_word = line2.substr( result_q, line2.find( "&amp;spell=1" ) - result_q );
				return boost::optional< string >( if_word );
			}
		}
	}
	return boost::optional< string >();
}

int main()
{
	int count = 0;
	std::string word = "q=C%2B%2BTemplateテクニック";
	while( 1 )
	{
		const auto search_result =  my_search( word );

		if( search_result )
		{
			word = *search_result;
			std::cout << ++count << std::endl;
		}
		else
			break;
	}

	std::cout << "Total : " << count << std::endl;

	return 0;
}

こういうのはノリと鮮度なので無駄が多いのは許して><。
アレですね、boost::asioはシリアル通信でお世話になることが多く、http通信で使うことは滅多にないのでほぼletsboostさんのコードをそのまま使いました。でも面白かったのでもうちょっといじってみたいなぁなんて思ったり。こういう機会を与えてくれる日常(及びTL)に感謝です!幸せ♪

//2011.7.24追記
どうやらGoogleさんがこの現象になるのを修正しちゃったみたいです。今は"もしかして"って出てきません。