Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:50:39+00:00 2026-05-22T19:50:39+00:00

why do we need a generic type specified in Boost library? Isn’t template enough?

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:50:39+00:00Added an answer on May 22, 2026 at 7:50 pm

    Because C++ does not have generic type. It has type templates, which are compiled for each substituted type separately. The expression

    vector v;
    

    is a syntax error, because vector is not a class (it’s a class template that can be instantiated to class by giving it template parameters).

    There is a somewhat generic type in C++, the void*, but then it’s your responsibility to remember what you stored in it (esp. for purposes of deletion). The boost::any is a typesafe alternative that remembers what you stored there and will give an error if you try to convert it to something it does not have defined conversion to (you still have to ask it for the actual value by any_cast).

    As for operator=, yes, it is used by the container. The standard containers generally require the element type to be default constructible and assignable, which means they need to have working copy constructor and assignment operator. They generally either don’t have to be default constructible or only have to be default constructible if you use certain operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++/CLI code I need to check if the type is a specific generic
I need a generic container that keeps its elements sorted and can be asked
I need to enumerate though generic IList<> of objects. The contents of the list
I need to develop a generic jQuery-based search plugin for the ASP.NET MVC application
I need a quick algorithm to select 5 random elements from a generic list.
I'm creating a generic class and in one of the methods I need to
I have an abstract generic class BLL<T> where T : BusinessObject . I need
I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T>
I need to remove all entries in a dictionary accordingly to a specified lower
I'm working on a generic delegate function and declaring a return type of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.