Visual Studio2017不支持C11新特性_Generic

Visual Studio 2017 does not supportC11 new feature _Generic

谁能告诉我为什么Visual Studio 2017 不支持C11 新功能_Generic?我发现这是一个非常有用的功能,但不能在 Visual Studio 2017 年使用。

下面是示例代码:

#include <stdio.h>
#define MYTYPE(X) _Generic((X),\
int:"int",\
float:"float",\
double:"double",\
default:"other")

int main(void)
{
      int d = 5;
      printf("%s\n", MYTYPE(d));
      printf("%s\n", MYTYPE(2.0*D));
      return 0;
}

编译器会给出以下警告和错误:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>predef.c
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(11): warning C4013: '_Generic' undefined; assuming extern returning int
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(11): error C2059: syntax error: 'type'
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(12): error C2065: 'D': undeclared identifier
1>c:\users\mia\documents\c\listing 16.13\project1\project1\predef.c(12): error C2059: syntax error: 'type'
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Visual Studio 2017 不支持任何 C11 功能。您可以将 Intel C 编译器 插入 Visual Studio(支持 C11)或使用 ClangGcc.

Quora 答案:

  • To your Visual Studio add the inbuilt CLANG support
  • Create new project
  • In project properties (General) for “Platform Toolset” select “Visual Studio 2017 - Clang with Microsoft CodeGen (v141_clang_c2)
  • In C/C++ “Language” section open the “C language” standard list , where you will find all the C standards relevant todayL C89, C99, C11 …select the one you wish

After this (same as ever) VS2017 will compile files with “.c” extension as C, and “.cpp” extension as C++.

Do not forget that one can change the properties on the per file basis too.

这是因为微软从来没有把符合C语言标准放在首位。在过去 20 年左右的时间里,他们的主要关注点一直是 C++。所以Visual Studio算是C++编译器吧。

在"C mode"中,它对第一个C标准C90的遵从性仍有疑问。他们花了很长时间才尝试获得 C99 合规性,他们只是在最近几年才为此做出了一些努力。它仍然不完全符合 C99。据我所知,目前还没有 C11 或 C17 合规性计划。

因此,如果标准一致性对您很重要,您必须寻找另一个 C 编译器。