C++14的错误,C++17的LNK2019和C1001总在一起

4

有一个非常简单的3文件源代码。

使用cl.exe编译。

  • 使用/std:c++17编译时,会出现c++14错误(error C3533: 参数不能具有包含'auto'的类型)。
  • 使用/std:c++20编译时,会出现LNK2019
  • 当使用/std:c++20时,并且将#include <string>serialization.cpp移动至database.h,则会出现致命的内部编译器错误C1001

源文件:

database.h:

#pragma once

#include <map>

struct map_manip 
{
    void operator()(auto& map, uint8_t size = 2);
};

database.cpp:

#include "database.h"

void map_manip::operator()(auto& map, uint8_t size)
{
}

serialization.cpp:

#include "database.h"

#include <string>

struct entry
{
    std::map<std::string_view, uint32_t> units;
};

int main()
{
    entry x;
    map_manip y;
    y(x.units);
}

使用c++17编译:

D:\dev\Local\Test>cl /EHsc /std:c++17 serialization.cpp database.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

serialization.cpp
D:\dev\Local\Test\database.h(8): error C3533: a parameter cannot have a type that contains 'auto'
serialization.cpp(12): error C2664: 'void map_manip::operator ()(unknown-type,uint8_t)': cannot convert argument 1 from 'std::map<std::string_view,uint32_t,std::less<std::string_view>,std::allocator<std::pair<const std::string_view,uint32_t>>>' to 'unknown-type'
D:\dev\Local\Test\database.h(8): note: see declaration of 'map_manip::operator ()'
database.cpp
D:\dev\Local\Test\database.h(8): error C3533: a parameter cannot have a type that contains 'auto'
database.cpp(3): error C3533: a parameter cannot have a type that contains 'auto'
Generating Code...

我相信这些错误在中是有效的,但由于我们使用编译,这不应该是情况。如果我理解错误,请纠正我。


使用c++20编译:

D:\dev\Local\Test>cl /EHsc /std:c++20 serialization.cpp database.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

serialization.cpp
database.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 14.29.30133.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:serialization.exe
serialization.obj
database.obj
serialization.obj : error LNK2019: unresolved external symbol "public: void __thiscall map_manip::operator()<class std::map<class std::basic_string_view<char,struct std::char_traits<char> >,unsigned int,struct std::less<class std::basic_string_view<char,struct std::char_traits<char> > >,class std::allocator<struct std::pair<class std::basic_string_view<char,struct std::char_traits<char> > const ,unsigned int> > > >(class std::map<class std::basic_string_view<char,struct std::char_traits<char> >,unsigned int,struct std::less<class std::basic_string_view<char,struct std::char_traits<char> > >,class std::allocator<struct std::pair<class std::basic_string_view<char,struct std::char_traits<char> > const ,unsigned int> > > &,unsigned char)" (??$?RV?$map@V?$basic_string_view@DU?$char_traits@D@std@@@std@@IU?$less@V?$basic_string_view@DU?$char_traits@D@std@@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string_view@DU?$char_traits@D@std@@@std@@I@std@@@2@@std@@@map_manip@@QAEXAAV?$map@V?$basic_string_view@DU?$char_traits@D@std@@@std@@IU?$less@V?$basic_string_view@DU?$char_traits@D@std@@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string_view@DU?$char_traits@D@std@@@std@@I@std@@@2@@std@@E@Z) referenced in function _main
serialization.exe : fatal error LNK1120: 1 unresolved externals

说实话,我完全不理解这个错误,但似乎它与“auto”有些关联。即使将“auto”更改为“template”也没有改变任何事情,但是如果我们从“void operator()”参数中移除“auto& map”(在database.hdatabase.cpp两个文件中),那么它就可以编译为.exe文件了!

使用c++20编译并将#include <string>serialization.cpp移到database.h

database.h:

#pragma once

#include <map>
#include <string>

struct map_manip 
{
    void operator()(auto& map, uint8_t size = 2);
};

serialization.cpp:

#include "database.h"

struct entry
{
    std::map<std::string_view, uint32_t> units;
};

int main()
{
    entry x;
    map_manip y;
    y(x.units);
}

在这种情况下,我只会得到一个内部编译器错误。我感觉应该向MSVC开发人员报告此问题,但仍然在此处提供,以防万一。
D:\dev\Local\Test>cl /EHsc /std:c++20 serialization.cpp database.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

serialization.cpp
serialization.cpp(5): fatal error C1001: Internal compiler error.
(compiler file 'msc1.cpp', line 1603)
 To work around this problem, try simplifying or changing the program near the locations listed above.
If possible please provide a repro here: https://developercommunity.visualstudio.com
Please choose the Technical Support command on the Visual C++
 Help menu, or open the Technical Support help file for more information
INTERNAL COMPILER ERROR in 'C:\dev\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\cl.exe'
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information

非常感谢您的任何想法。

1个回答

7

非常感谢!我已经向微软报告了这个问题。 我还进行了一些更改并使用CLang测试了代码: ~ cl.exe崩溃,无论函数实现是否在头文件中。 ~ 另一方面,使用您的建议(1和2)实现后,CLang可以完美编译。 - Ivan Malivanchuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接