I recently yet again encountered the notation
( const int[10] ){ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
As I recall it's permitted in both C and C++, but via quite different language mechanisms.
I believed that in C++ the formal view is that it's a construction of an unnamed temporary via an epxlicit type conversion (T)
cast-expression that would reduce to a static_cast
, that constructs an object via C++11 §5.2.9/4:
” an expression
e
can be explicitly converted to a typeT
using astatic_cast
of the formstatic_cast<T>(e)
if the declarationT t(e);
is well-formed, for some invented temporary variablet
(8.5)
However, the cast-expression syntax is defined by C++11 §5.4/2 as being either unary-expression or, recursively, a (
type-id )
cast-expression, where the single base case is reduction to unary-expression.
And as far as I can tell a braced init-list is not an expression?
An alternative view could be that it’s an explicit type conversion via functional notation, C++11 §5.2.3/3,
” a simple-type-specifier or typename-specifier followed by a braced-init-list creates a temporary object of the specified type
but as far as I can tell a simple-type-specifier can’t involve parentheses, and a typename-specifier involves the keyword typename
?
Alias<T[N]>{...}
. – HagberryAlias
supposed to do? – Thithertotemplate<class T> using Alias = T;
, a simply using-alias to make the code parseable. – Hagberryidentity
for that. – Mho-pedantic
was specified. Without-pedantic
g++ accepted the code, presumably as a C99 compound literal used as a C++ extension. – ClaybourneType
, to my mind self-descriptive. For earlier versions of Visual C++ I usedType_T_
, which wastemplate< class Type_ > struct Type_T_{ typedef Type_ T; };
, with ditto more verbose usage. – Claybourne