C++11 constexpr causes compiler's internal error (C1001)

别等时光非礼了梦想. 提交于 2019-12-10 21:01:55

问题


I am using Visual Studio 2015 Update 3.

I get a fatal error:

(code C1001) : An internal error has occurred in the compiler.

Here is the code :

template<typename T>
constexpr T epsilon = std::numeric_limits<T>::epsilon();

I read it was fixed in Visual Studio Update 2. Can someone explain me why I am getting this error? Thanks in advance.


回答1:


Any internal error (ICE) is a compiler bug. You get it because you have happened to trigger that bug. For this compiler you can report it at Microsoft Connect.

For such a report you need an example with an expected correct result, and the erroneous result.

The following test program compiles & runs nicely with MinGW g++ 5.1

#include <limits>

template<typename T>
constexpr T epsilon = std::numeric_limits<T>::epsilon();

#include <iostream>
using namespace std;
auto main() -> int
{
    cout << epsilon<double> << endl;
}

Output:

2.22045e-016

With Visual C++ 2015 update 2 it produces an ICE:

foo.cpp(10): fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'f:\dd\vctools\compiler\cxxfe\sl\p1\c\symbols.c', line 28114)
 To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
 Help menu, or open the Technical Support help file for more information
foo.cpp(10): note: see reference to variable template 'const double epsilon' being compiled

Compiler version:

> cl /nologo- 2>&1 | find "++"
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23725 for x86



回答2:


I've raised this as a bug with Microsoft, but they have had a fix since early 2017 which has not been released from what I can see as of today.

I've also provided a project on GitLab and given information to Microsoft for this project here: https://gitlab.com/cppocl/tostring

Loading the .sln and compiling currently crashes with Visual Studio 2015 update 2 or 3, and Visual Studio Enterprise 2017 version 15.3.1.

It does seem that the combination of template and constexpr causes the compiler to crash.

I've seen reports for Visual Studio 2017 describing similar types of problems.

This link says fixed pending release: https://developercommunity.visualstudio.com/content/problem/18155/msvc-2017-c-fatal-error-c1001-constexpr-initializa.html

Visual Studio 2015 backlog of bugs relating to constexpr is here: https://blogs.msdn.microsoft.com/vcblog/2015/12/02/constexpr-in-vs2015-update-1/

EDIT: I also don't believe changing optimization settings will make any difference, as has been recommended in other posts. I have experimented with these settings and applied recommended patches without success so far.



来源:https://stackoverflow.com/questions/38696112/c11-constexpr-causes-compilers-internal-error-c1001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!