C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

typedefs vs. using-declarations

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
pyramus
Guest





PostPosted: Mon Nov 28, 2005 8:13 pm    Post subject: typedefs vs. using-declarations Reply with quote



I am wondering which one should be preferred for creating a local
synonym within a function definition. Let's say I have a type in my
library called MyLib::Foundation::int32, which I can't stand typing all
the time. Should I do:

void foo()
{
typedef MyLib::Foundation::int32 int32;
int32 x = 0;
}

OR:

void bar()
{
using MyLib::Foundation::int32;
int32 x = 0;
}

First, is there any difference between the two, according to the
language standard? A colleague has suggested that typedef would
"override" any other definitions of int32 that might exist, while a
using-declaration may not, but he wasn't sure about this.

If there is a difference, would there be one that you'd prefer as a best
practice? (When restricted to local use within function definitions.)

Thanks in advance
Back to top
Victor Bazarov
Guest





PostPosted: Tue Nov 29, 2005 5:35 am    Post subject: Re: typedefs vs. using-declarations Reply with quote



pyramus wrote:
Quote:
I am wondering which one should be preferred for creating a local
synonym within a function definition. Let's say I have a type in my
library called MyLib::Foundation::int32, which I can't stand typing
all the time. Should I do:

void foo()
{
typedef MyLib::Foundation::int32 int32;
int32 x = 0;
}

OR:

void bar()
{
using MyLib::Foundation::int32;
int32 x = 0;
}

First, is there any difference between the two, according to the
language standard? A colleague has suggested that typedef would
"override" any other definitions of int32 that might exist, while a
using-declaration may not, but he wasn't sure about this.

No, it won't "override". It will _hide_. And a using declaration
will actually introduce a conflict, an ambiguity, IIRC. Just test
it with your favourite compiler.

Quote:
If there is a difference, would there be one that you'd prefer as a
best practice? (When restricted to local use within function
definitions.)

There is no essential difference to the code that uses it. Of course,
if you for some reason change 'int32' from being a type to being
an object, then 'using' should still compile, while 'typedef' should
definitely fail.

V



Back to top
pyramus
Guest





PostPosted: Tue Nov 29, 2005 3:58 pm    Post subject: Re: typedefs vs. using-declarations Reply with quote



Victor Bazarov wrote:
Quote:
First, is there any difference between the two, according to the
language standard? A colleague has suggested that typedef would
"override" any other definitions of int32 that might exist, while a
using-declaration may not, but he wasn't sure about this.


No, it won't "override". It will _hide_. And a using declaration
will actually introduce a conflict, an ambiguity, IIRC. Just test
it with your favourite compiler.


Oops, yeah, "hide" is the correct term. I tested this on my compiler
(VC2003) and found that both typedefs and using-declarations produced
similar results. If they referred to two different things, then the
compiler would produce an error.

If the using-declaration comes after the typedef:
error C2874: using-declaration causes a multiple declaration of
'MyLib::Foundation::int32'

If the typedef comes after the using-declaration:
error C2371: 'MyLib::Foundation::int32' : redefinition; different basic
types

However, if both referred to the same thing, the compiler did not
produce an error or warning. So it looks to me like there is little or
no practical difference between the two in terms of name look up.
According to section 3.4.1 of the standard,

"The name look up rules apply uniformly to all names (including
typedef-names, namespace-names and class-names) wherever the grammar
allows such names in the context discussed by a particular rule."

Quote:
If there is a difference, would there be one that you'd prefer as a
best practice? (When restricted to local use within function
definitions.)


There is no essential difference to the code that uses it. Of course,
if you for some reason change 'int32' from being a type to being
an object, then 'using' should still compile, while 'typedef' should
definitely fail.


Why? Let's say you changed int32 to:

namespace MyLib
{
namespace Foundation
{
class int32 { ... };
}
}

Then would it make any difference whether you used

typedef MyLib::Foundation::int32 int32;

or

using MyLib::Foundation::int32;

In either case, 'int32' would just become a synonym for the same class
name, right?

Thanks again

Back to top
Victor Bazarov
Guest





PostPosted: Tue Nov 29, 2005 6:08 pm    Post subject: Re: typedefs vs. using-declarations Reply with quote

pyramus wrote:
Quote:
Victor Bazarov wrote:
[...]
There is no essential difference to the code that uses it. Of course,
if you for some reason change 'int32' from being a type to being
an object, then 'using' should still compile, while 'typedef' should
definitely fail.


Why? Let's say you changed int32 to:

namespace MyLib
{
namespace Foundation
{
class int32 { ... };
}
}

Then would it make any difference whether you used

typedef MyLib::Foundation::int32 int32;

or

using MyLib::Foundation::int32;

In either case, 'int32' would just become a synonym for the same class
name, right?

Right. But now, change it to

namespace MyLib
{
namespace Foundation
{
const int int32 = 32; // an object
}
}

V

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.