why do we need a generic type specified in Boost library? Isn't template enough? Fore example, if I want a container of a specific type, I would just do:
template<class Type>
vector<Type> v;
If I want to specify a container which contains everything, I simply write:
vector v;
In explanation for boost::any (http://www.boost.org/doc/libs/1_46_1/doc/html/any/s02.html)
Converting types that can hold one of a number of possible value types, e.g. int and string, and freely convert between them, for instance interpreting 5 as "5" or vice-versa. Such types are common in scripting and other interpreted languages. boost::lexical_cast supports such conversion functionality.
Why do we need implicit type like in scripting languages such as PHP?
Further more, in the boost::any example, why:
using boost::any_cast;
typedef std::list<boost::any> many;
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
is acceptable? Does the container use the operator = implemented in boost::any?
any & operator=(const any &);
which makes boost::any able to hold any type? Operator = defined in boost::any is explained as:
Effects: Copies content of rhs into current instance, discarding previous content, so that the new content is equivalent in both type and value to the content of rhs, or empty if rhs.empty().
Throws: std::bad_alloc or any exceptions arising from the copy constructor of the contained type. Assignment satisfies the strong guarantee of exception safety.
http://www.boost.org/doc/libs/1_46_1/doc/html/boost/any.html