c++ - C++17、LNK2019、C1001 中的 C++14 错误
问题描述
有一个非常简单的 3 文件源。
用cl.exe编译。
- 使用/std:c++17编译时出现c++14错误(错误 C3533:参数不能具有包含 'auto' 的类型)。
- 使用/std:c++20它编译但给出LNK2019。
- 使用/std:c++20时,但 WITH
#include <string>
从serialization.cpp => database.h移动,它会给出致命的内部编译器错误 C1001。
源文件:
数据库.h:
#pragma once
#include <map>
struct map_manip
{
void operator()(auto& map, uint8_t size = 2);
};
数据库.cpp:
#include "database.h"
void map_manip::operator()(auto& map, uint8_t size)
{
}
序列化.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++14中是有效的,但在我们使用c++17编译时不应该是这种情况。如果我弄错了,请纠正我。
用 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并不会改变任何事情,但如果我们auto& map
从void operator()
参数(database.h和database.cpp)中删除,那么它编译为 .exe 就好了!
使用 c++20 编译并#include <string>
从serialization.cpp => database.h移动
数据库.h:
#pragma once
#include <map>
#include <string>
struct map_manip
{
void operator()(auto& map, uint8_t size = 2);
};
序列化.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
任何想法将不胜感激。
解决方案
auto
在函数参数列表中创建一个缩写函数模板,一个 C++20 特性- 由于函数是模板,所以需要像往常模板一样,将函数定义移到表头,以进行程序链接;请参阅为什么模板只能在头文件中实现?
- 将 ICE 报告到https://developercommunity.visualstudio.com/home或通过帮助 > 发送反馈 > 报告问题...从 Visual Studio
推荐阅读
- html - 悬停第n个元素时如何将css应用于前n个元素
- python-3.x - 如何在 fritzconnect 中发送 call_action
- python - 破碎的绘图铅笔
- javascript - Angular 如何更新库 metadata.json 文件
- angular - 大写指令不更新输入角度
- android - 警告:API 'variant.getJavaCompile()' 已过时,已替换为 'variant.getJavaCompileProvider()'
- windows - 将“//”替换为“;” 使用 sed
- dart - .template.dart 文件未创建
- java - Android - JSONArray 上的 toString 使用双引号和反斜杠使 json 无效
- java - 无法找到明确的活动类您是否在 AndroidManifest.xml 中声明了此活动?