473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

typedefs vs. using-declarations

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
Nov 28 '05 #1
3 4467
pyramus wrote:
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.
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
Nov 29 '05 #2
Victor Bazarov wrote:
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."
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
Nov 29 '05 #3
pyramus wrote:
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
Nov 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Levent | last post by:
Please consider the following Parent and Child template classes: template <class T> class Parent { public: typedef T type; typedef T& ref; /* pack of typedefs */ };
9
by: bubbakittee | last post by:
I am designing a package of functions to be used by people with very little programming experience (if any) and very little patience. I don't want to scare them with programmerish words like...
0
by: Nadav | last post by:
Hi, I am using the unmanaged reflection API ( e.g. IMetaDataImport, IMetaDataEmit, … ) and I wonder…. I couldn’t find a way renaming a Field/Method name… How could I do that? Concerning...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
24
by: hammer1234 | last post by:
Hi I am wondering if there is a way of using the underlying bit representations of floats. I am interested in creating a violation in MISRA C:2004 of rule "The underlying bit representations...
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
2
by: Chris Fairles | last post by:
I'm wondering if theres any rationale behind using _t vs _type in typedefs. I took a look in the standard and it doesn't say much besides things like "size_type must be size_t", "difference_type...
2
by: =?Utf-8?B?VGhlQ3VyaW91c09uZQ==?= | last post by:
I have this situation where I need to access the #defines and typedefs defined in a C++ header file from a .NET Project (specifically C#). The C++ header file is from a medical device manufacturer...
1
by: pauldepstein | last post by:
While reading some source code, I saw a variable called "end" of type "time". So I investigated what the type "time" meant and saw that time was a typedef for "Real". So what does "Real" mean? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.