typedef

P

Phlip

Gurikar said:
Does typedef vector<std::string> vNames; creates vector object?

It instantiates the vector template, creating a type called vNames, which
can generally be used as a class. No object yet.

vNames names; creates an object.
 
C

Chris \( Val \)

| Does typedef vector<std::string> vNames; creates vector object?

typedef' in themselves do not create any objects,
they create 'synonyms' - AKA: A symbolic substitute,
also known as an 'alias' for another object.

Objects are instantiated later down the track:

This:
typedef vector<std::string> vNames;

....introduces a 'synonym' called: 'vNames'

The following:
vNames vNamesObject;

....instantiates the actual object.

Cheers,
Chris Val
 
P

Phlip

Chris said:
typedef' in themselves do not create any objects,
they create 'synonyms' - AKA: A symbolic substitute,
also known as an 'alias' for another object.

Objects are instantiated later down the track:

This:
typedef vector<std::string> vNames;

In the special case of a template, typedef-ing also instantiates the target
class, meaning certain lookup paths from inside the template to external
identifiers get nailed down. By contrast...

#define vNames vector<std::string>

would reinstantiate the template each time vNames is used. I suspect that
can introduce subtle bugs.
 
R

Rolf Magnus

Chris said:
| Does typedef vector<std::string> vNames; creates vector object?

typedef' in themselves do not create any objects,
they create 'synonyms' - AKA: A symbolic substitute,
also known as an 'alias' for another object.

No. It creates an alias for another type, not for an object.
 
C

Chris \( Val \)

| Chris ( Val ) wrote:
|
| > typedef' in themselves do not create any objects,
| > they create 'synonyms' - AKA: A symbolic substitute,
| > also known as an 'alias' for another object.
| >
| > Objects are instantiated later down the track:
| >
| > This:
| > typedef vector<std::string> vNames;
|
| In the special case of a template, typedef-ing also instantiates the target
| class,

Are you sure about that ?

I have not heard of a typedef being responsible
for the actual instantiation of any object(s) ?

| meaning certain lookup paths from inside the template to external
| identifiers get nailed down. By contrast...

I'm not sure what you mean by that.
Can you please elaborate a little further ?

| #define vNames vector<std::string>
|
| would reinstantiate the template each time vNames is used.

Are you certain of this ?
Hmn..., maybe I'm misunderstanding what you mean ?

| I suspect that can introduce subtle bugs.

Ok, here are a couple of tests - Please feel free to
correct me where ever I have erred:

# include <iostream>
# include <ostream>
# include <vector>

namespace Std
{
template<class T> struct MyString
{
MyString() { std::cout << "Ctor-1 MyString\n"; }
MyString( T ) { std::cout << "Ctor-2 MyString\n"; }
MyString( const MyString& )
{ std::cout << "Copy-Ctor MyString\n"; }
};
}

int main()
{
#define vNames std::vector<Std::MyString<int> >
typedef std::vector<Std::MyString<int> > VecMyStr;

VecMyStr V1;
//V1.push_back( Std::MyString<int>() );
//V1.push_back( Std::MyString<int>( 5 ) );

vNames V2;
//V2.push_back( Std::MyString<int>() );
//V2.push_back( Std::MyString<int>( 10 ) );

return 0;
}

In both cases, no objects are constructed until the
'push_back()' member of std::vector is invoked. You
can also verify that the vector size is zero in each
test case.

Can you please explain via an example, exactly what
you were trying to convey if not the above ?

Cheers,
Chris Val
 
C

Chris \( Val \)

| Chris ( Val ) wrote:
|
| > | Does typedef vector<std::string> vNames; creates vector object?
| >
| > typedef' in themselves do not create any objects,
| > they create 'synonyms' - AKA: A symbolic substitute,
| > also known as an 'alias' for another object.
|
| No. It creates an alias for another type, not for an object.

Correct.
To be even more pedantic:
7.13: A typedef-name is thus a synonym for another type.

Cheers,
Chris Val
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top