[bind10-dev] cc message format description and routing daemon added to svn

韩枫 hanfeng at cnnic.cn
Sun Sep 27 03:44:55 UTC 2009


If you want to do something in c++ which has the same effect like the 
following ruby code:
case item
    when Hash
       ret = pack_hash(item)
    when Array
       ret = pack_array(item)
 end

You can use template, but template is static which is done in compile 
type so that the type you want to deal with should be limited.
template <typename Data>
string encode(const Data &data);

template <>
string encode<std::string>(const string &data)
{
    std::cout << "encode string\n";
    return data;
}

template <>
string encode<list<string> >(const list<string>  &l)
{
    std::cout << "encode list\n";
    std::ostringstream os;
    for (list<string>::const_iterator i = l.begin(); i != l.end(); ++i)
        os << *i;

    return os.str();
}

//other staff like map..
...

list<string> list_data;
string encode_list_msg = encode(list_data);

string str_data;
string encode_str_msg = encode(str_data);

The compiler will do the dispatch for you. And if one type is out of 
your implementation, you will get compile error.




More information about the bind10-dev mailing list