我有以下代码:
#include <array>
#include <iostream>
class ExternalGeometryExtension
{
public:
enum Flag {
Defining = 0,
Frozen = 1,
Detached = 2,
Missing = 3,
Sync = 4,
NumFlags
};
constexpr static std::array<const char *,NumFlags> flag2str{{ "Defining", "Frozen", "Detached","Missing", "Sync" }};
};
int main()
{
std::cout << ExternalGeometryExtension::flag2str[ExternalGeometryExtension::Frozen] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
可以使用以下版本编译良好:clang版本5.0.0和gcc(Ubuntu 4.8.4-2ubuntu1〜14.04.3)4.8.4
无法使用MSVC2013进行编译。
编译错误是:
#include <array>
#include <iostream>
class ExternalGeometryExtension
{
public:
enum Flag {
Defining = 0,
Frozen = 1,
Detached = 2,
Missing = 3,
Sync = 4,
NumFlags
};
constexpr static std::array<const char *,NumFlags> flag2str{{ "Defining", "Frozen", "Detached","Missing", "Sync" }};
};
int main()
{
std::cout << ExternalGeometryExtension::flag2str[ExternalGeometryExtension::Frozen] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
完整的编译器输出在这里
我的问题是:我做错了什么?为什么不使用MSVC2013进行编译?
我可以做些什么使该代码与MSVC2013一起使用,而又不会在其他编译器中破坏它?
EDIT: I have changed the code so that it is a Minimal, Complete, and Verifiable example as requested by Toby Speight based on the good guess of Diodacus. I cannot produce the error output of that specific code, because I do not have a copy of MSVC2003. I work on an opensource, FreeCAD, which offers Windows support. I use Linux. In any case, the errors in the output correspond to the code I show. This is the output of the AppVeyor test before integration. The code passes the Linux CI fine. I am going to try to make the most of this question, hopping that it is useful for others.
EDIT 2: I have realised that the double bracket initialization has raised some eyebrows. From the example in cppreference:
在CWG 1270修订版之前的C ++ 11中需要双括号(修订版之后的C ++ 11中以及C ++ 14及更高版本中不需要)
没有双括号,gcc 4.8失败。
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |