具有自动参数的函数使用GCC进行编译,但不使用Visual C ++进行编译 [英] Function with auto parameter compiles using GCC but does not compile using Visual C++

查看:53
本文介绍了具有自动参数的函数使用GCC进行编译,但不使用Visual C ++进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段:

#include <iostream> 

void print(auto arg) {
  std::cout << arg << std::endl;
}

int main() {
  print("Hi");

  return 0;
}

您可以在此处看到( https://godbolt.org/z/2GSrXs )GCC代码可以编译并正常运行.

As you can see here (https://godbolt.org/z/2GSrXs) using GCC the code compiles and runs fine.

您可以在此处看到( https://godbolt.org/z/rtR6w9 )Visual C ++代码无法编译,并导致错误消息:

As you can see here (https://godbolt.org/z/rtR6w9) using Visual C++ the code does not compile and results in the error message:

< source>(3):错误C3533:参数不能具有包含"auto"的类型.

<source>(3): error C3533: a parameter cannot have a type that contains 'auto'.

在我看来,此功能尚未在Visual C ++中实现.此外,我无法在以下列表中找到编译器功能: https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019 .因此,我不确定Microsoft开发人员是否在其待办事项列表"中具有该功能?

It seems to me that this feature has not jet been implementet in Visual C++? Additionally I was not able to find the compiler feature in the following list: https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019. So I am not sure if the Microsoft developer have the feature on their ToDo List?

推荐答案

自动接收功能是C ++ 20的功能.必须使用/std:latest 启用它.但是,MSVC目前仅部分支持此语法,因此并非所有使用此功能的声明都可以使用.

Functions receiving auto is a C++20 feature. It must be enabled with /std:latest. However, MSVC only has partial support for this syntax at this time, so not all declaration using this feature will work.

从C ++ 14天开始,GCC就一直支持自动输入函数参数.

GCC has supported auto in function parameters since early C++14 days as an extension.

您可以阅读 P1141R2:约束声明的另一种方法

这篇关于具有自动参数的函数使用GCC进行编译,但不使用Visual C ++进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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