function's definition:
void foo(const char*) {}
foo(char[16]{}); //houston, there is a problem!
foo(type_alias<char[16]>{}); //compile happily
type_alias
is simple:
template<typename T>
using type_alias = T;
live demon
As comment notes, case 1
cannot compile while case 2
can.
I know that alias declarations
with using
is not text substitution(like #define
) and it is a synonym for the type.
But I still cannot figure out how to account for this situation. Then I give GCC a try
:
prog.cc: In function 'int main()':
prog.cc:11:7: error: expected primary-expression before 'char'
foo(char[16]{});
^~~~
prog.cc:12:7: error: taking address of temporary array
foo(type_alias<char[16]>{});
^~~~~~~~~~~~~~~~~~~~~~
Ah, GCC gave me an error instead! Then I compile it with different versions of the two compilers:
case 1
is: prog.cc:11:11: error: expected '(' for function-style cast or type construction
foo(char[16]{}); ~~~~^
Clang lets case 2
pass.
GCC's forbid both the two cases to pass competition. Error messages for case 1
and case 2
have been listed above.
BTW, for Clang, I have also tested with pedantic-errors
, but nothing changed.
Questions:
case 2
: Clang, GCC, who conforms the standard? Any spec in standard(language lawyer)?case 1
: Whose error message is more correct(IOW, conforms the standard)?As VTT comments, for case 1
, it should be foo(const char[16]{});
. Apology for this mistake.
But Clang can compile foo(type_alias<char[16]>{});
. It seems to be a bug?
Well, type_alias<cv T>{}
is equivalent to (cv T){}
, not to cv T{}
. This distinction matters when T
is an array:
foo((const char[16]){}); // OK
foo(type_alias<const char[16]>{}); // OK
foo(const type_alias<char>[16]{}); // KO
foo(const char[16]{}); // KO
Demo: https://wandbox.org/permlink/KGf3HVqN3USq6yy8
For case 2: Clang, GCC, who conforms the standard? Any spec in standard(language lawyer)?
Both does, both accept foo(type_alias<char>[16]{})
, but gcc warns you about it (and since you compiled with -Werror
, this warning is turned into an error ;).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With