函数参数中的`type_alias< char [N]> {}与VS`char [N] {}` [英] `type_alias<char[N]>{}` VS `char[N]{}` in function's argument

查看:112
本文介绍了函数参数中的`type_alias< char [N]> {}与VS`char [N] {}`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 环境:x86-64 Clang 6.0.0
  • Environment: x86-64 Clang 6.0.0

函数的定义:

void foo(const char*) {}

  1. foo(char[16]{}); //houston, there is a problem!
  2. foo(type_alias<char[16]>{}); //compile happily
  1. foo(char[16]{}); //houston, there is a problem!
  2. foo(type_alias<char[16]>{}); //compile happily

type_alias很简单:

template<typename T>
using type_alias = T;

> live demon

如注释所述,case 1无法编译,而case 2可以编译.

As comment notes, case 1 cannot compile while case 2 can.

我知道带有usingalias declarations不是文本替换(例如#define),并且是该类型的同义词.

I know that alias declarations with using is not text substitution(like #define) and it is a synonym for the type.

但是我仍然不知道如何解决这种情况.然后我给 GCC 一个 try :

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:

  • Clang case 1错误消息是:
  • Clang's error message for case 1 is:

prog.cc:11:11:错误:函数样式转换或类型构造应为'('

prog.cc:11:11: error: expected '(' for function-style cast or type construction

foo(char[16]{});
      ~~~~^

  • Clang 使case 2通过.

    • Clang lets case 2 pass.

      海湾合作委员会禁止两个案件均通过竞争. case 1case 2的错误消息已在上面列出.

      GCC's forbid both the two cases to pass competition. Error messages for case 1 and case 2 have been listed above.

      顺便说一句,对于 Clang ,我也用pedantic-errors进行了测试,但没有任何变化.

      BTW, for Clang, I have also tested with pedantic-errors, but nothing changed.

      问题:

      • 对于case 2: Clang GCC ,谁符合标准?标准(语言律师)有任何规定吗?
      • 对于case 1:谁的错误消息更正确(IOW,符合标准)?
      • For case 2: Clang, GCC, who conforms the standard? Any spec in standard(language lawyer)?
      • For case 1: Whose error message is more correct(IOW, conforms the standard)?

      如VTT注释,对于case 1,它应该是foo(const char[16]{});.为这个错误表示歉意.

      As VTT comments, for case 1, it should be foo(const char[16]{});. Apology for this mistake.

      但是 Clang 可以编译foo(type_alias<char[16]>{});.好像是个虫子?

      But Clang can compile foo(type_alias<char[16]>{});. It seems to be a bug?

      推荐答案

      好吧,type_alias<cv T>{}等同于(cv T){},而不是cv T{}.当T是数组时,这种区别很重要:

      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
      

      演示: https://wandbox.org/permlink/KGf3HVqN3USq6yy8

      对于案例2:Clang,GCC,谁符合标准?标准(语言律师)有任何规定吗?

      For case 2: Clang, GCC, who conforms the standard? Any spec in standard(language lawyer)?

      两者都接受foo(type_alias<char>[16]{}),但是gcc 警告关于此问题(并且由于您使用-Werror进行编译,因此该警告变成了错误;).

      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 ;).

      这篇关于函数参数中的`type_alias&lt; char [N]&gt; {}与VS`char [N] {}`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆