All standard references below refers to N4659: March 2017 post-Kona working draft/C++17 DIS.
Typedef declarations can, whereas (until C++23) alias declarations cannot, be used as initialization statements
But, with the first two non-template examples, are
there any other subtle differences in the standard?
- Differences in semantics: none.
- Differences in allowed contexts(+):
- C++20 and earlier: some.
- C++23 and onwards: none(++).
(+) Not including the examples of alias templates, which has already been mentioned in the original post.
(++) P2360R0 (Extend init-statement to allow alias-declaration) has been approved by CWG and as of C++23, this inconsistency between typedef declarations and alias declarations will have been removed.
Same semantics
As governed by [dcl.typedef]/2 [extract, emphasis mine]
[dcl.typedef]/2 A
typedef-name
can also be introduced by an
alias-declaration.
The identifier following the using
keyword becomes a
typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. Such a
typedef-name has the same semantics as if it were introduced by the typedef
specifier. [...]
a typedef-name introduced by an alias-declaration has the same semantics as if it were introduced by the typedef
declaration.
Subtle difference in allowed contexts
However, this does not imply that the two variations have the same restrictions with regard to the contexts in which they may be used. And indeed, albeit a corner case, a typedef declaration is an init-statement and may thus be used in contexts which allow initialization statements
// C++11 (C++03) (init. statement in for loop iteration statements).
for (typedef int Foo; Foo{} != 0;)
// ^^^^^^^^^^^^^^^ init-statement
{
}
// C++17 (if and switch initialization statements).
if (typedef int Foo; true)
// ^^^^^^^^^^^^^^^ init-statement
{
(void)Foo{};
}
switch (typedef int Foo; 0)
// ^^^^^^^^^^^^^^^ init-statement
{
case 0: (void)Foo{};
}
// C++20 (range-based for loop initialization statements).
std::vector<int> v{1, 2, 3};
for (typedef int Foo; Foo f : v)
// ^^^^^^^^^^^^^^^ init-statement
{
(void)f;
}
for (typedef struct { int x; int y;} P; auto [x, y] : {P{1, 1}, {1, 2}, {3, 5}})
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ init-statement
{
(void)x;
(void)y;
}
whereas before C++23 (this answer may have prompted P2360R0 which addressed this niche subtlety in C++23) an alias-declaration is not an init-statement, and thus may not be used in contexts which allows initialization statements
// C++ 11.
for (using Foo = int; Foo{} != 0;) {}
// ^^^^^^^^^^^^^^^ error: expected expression
// C++17 (initialization expressions in switch and if statements).
if (using Foo = int; true) { (void)Foo{}; }
// ^^^^^^^^^^^^^^^ error: expected expression
switch (using Foo = int; 0) { case 0: (void)Foo{}; }
// ^^^^^^^^^^^^^^^ error: expected expression
// C++20 (range-based for loop initialization statements).
std::vector<int> v{1, 2, 3};
for (using Foo = int; Foo f : v) { (void)f; }
// ^^^^^^^^^^^^^^^ error: expected expression
typedef void (&MyFunc)(int,int);
orusing MyFunc = void(int,int);
? – Nebulositytypedef void MyFunc(int,int);
(which actually doesn't look as bad), orusing MyFunc = void(&)(int,int);
– Sabatierusing
form I presented. Indeed the first is not too bad (except for having the name in the middle). – Nebulosityusing MyFunc = void(&)(int,int);
? does it meanMyFunc
is a reference to a function? what if you omit the &? – Widnertypedef void (&MyFunc)(int,int);
. If you omit the&
it's equivalent totypedef void MyFunc(int,int);
– Sabatierusing
results in faster link times (don't remember which compiler he was talking about, though), because apparently the compiler generates shorter symbol names. I don't know if this holds up to scrutiny, though. – Assessmentusing
?struct Private; typedef int(Private::*Zero);
(Found in Qt's QFlags.h.) I simply cannot make sense of the syntax. – Edgyusing
overtypedef
for defining aliases – Emceeusing
is C++ only,typedef
can be used in C also. – Devol