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