UNAVOIDABLE situation for the use of vector<T>::value_type

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

For a type T, consider vector<T>::value_type. When is this member
'value_type' useful; in other words, when is the use of value_type
unavoidable?

Kindly clarify.

The reason for asking this question is the following:

For a particular type T, say std::string, Suppose I use,
vector<string>::value_type obj;
But, to initialize or assign a value to 'obj', we have to know the
actual type T. So we can simply declare
string obj;

In this example, the need for value_type is AVOIDABLE.

But there should be some genuine situation wherein the use of
value_type is UNAVOIDABLE. I am unable to come up with an example for
this unavoidable situation. Please clarify.

Thanks
V.Subramanian
 
T

Triple-DES

For a type T, consider vector<T>::value_type. When is this member
'value_type' useful; in other words, when is the use of value_type
unavoidable?

Kindly clarify.

The reason for asking this question is the following:

For a particular type T, say std::string, Suppose I use,
vector<string>::value_type obj;
But, to initialize or assign a value to 'obj', we have to know the
actual type T. So we can simply declare
string obj;

In this example, the need for value_type is AVOIDABLE.

But there should be some genuine situation wherein the use of
value_type is UNAVOIDABLE. I am unable to come up with an example for
this unavoidable situation. Please clarify.

template<typename Container>
void f(const Container& c)
{
if(c.empty() )
return;
// the only way to get the value_type at this point
Container::value_type t = *c.begin();
// ...
}

DP
 
J

Jerry Coffin

For a type T, consider vector<T>::value_type. When is this member
'value_type' useful; in other words, when is the use of value_type
unavoidable?

Kindly clarify.

The reason for asking this question is the following:

For a particular type T, say std::string, Suppose I use,
vector<string>::value_type obj;
But, to initialize or assign a value to 'obj', we have to know the
actual type T. So we can simply declare
string obj;

In this example, the need for value_type is AVOIDABLE.

But there should be some genuine situation wherein the use of
value_type is UNAVOIDABLE. I am unable to come up with an example for
this unavoidable situation. Please clarify.

Write a template to sum the elements in a container, and see if you
don't find a use for it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top