Visual Studio 2017 在外部"C"中使用模板时出现不正确的错误
Visual Studio 2017 Improper error on template use in extern "C"
考虑以下代码:
#include <vector>
#include <algorithm>
template<typename T, typename R, typename Op>
inline
std::vector<T>
transform_inline(const R & collection, Op op)
{
std::vector<T> result;
std::transform
(
std::begin(collection),
std::end(collection),
std::back_inserter(result),
op
);
return result;
}
extern "C"
{
void myFunc()
{
std::vector<std::pair<double,int>> data;
transform_inline<double>
(
data,
[](auto & o){ return o.first; }
);
}
}
它在GCC和Clang中编译,但是Visual Studio说:
<source>(31): error C2894: templates cannot be declared to have 'C' linkage
Microsoft (R) C/C++ Optimizing Compiler Version 19.10.25017 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
Compiler returned: 2
请参阅:https://godbolt.org/g/vgvl4t
通常,当您定义时,该错误通常是Extern" C"块中的模板,这显然不是这样。
看起来像一个视觉工作室错误...我正确吗?
任何已知的解决方法?
我不会混合声明和定义。以下是可编译的代码。
#include <vector>
#include <algorithm>
template<typename T, typename R, typename Op>
inline
std::vector<T>
transform_inline(const R & collection, Op op)
{
std::vector<T> result;
std::transform
(
std::begin(collection),
std::end(collection),
std::back_inserter(result),
op
);
return result;
}
extern "C"
{
void myFunc();
}
void myFunc()
{
std::vector<std::pair<double,int>> data;
transform_inline<double>
(
data,
[](auto & o){ return o.first; }
);
}
请参阅:https://godbolt.org/g/pbqcfc
相关文章:
- 失败,出现错误 87:参数不正确.如何判断哪个参数不正确?
- 宏定义不正确没有错误
- rpmbuild check-rpath 报告错误,指出路径不是绝对的,不正确的
- 对从引用到 std::reference_wrapper 或编译器错误的隐式转换的理解不正确?
- "this" Lambda 捕获的是不正确的。海湾合作委员会编译器错误?
- 对分段错误的理解不正确
- 收到错误 87,安装程序的参数不正确DiGetDeviceInterfaceDetail
- Visual Studio 2017 在外部"C"中使用模板时出现不正确的错误
- 将 SFINAE 上下文中不正确的模板实例化的硬错误转变为软错误
- 测试整数是否在范围内时,逻辑错误和不正确输出
- 带有绝对路径的LoadLibrary返回不正确的HMODULE,没有任何错误
- unordered_map不正确使用或错误
- memcpy错误,长度不正确
- 此功能可能出现哪些错误或不正确?
- 由于转换错误而导致的过载/专业不正确
- 我正在尝试遍历 20x20 网格并计算产品,但总是得到不正确的结果。你能帮忙发现我的错误吗?
- LoadLibraryEx错误87(参数不正确)
- 错误:隐式删除,因为默认定义格式不正确(结构向量)
- 使用 qsort() 时对包含C++字符串的类进行不正确/错误的排序
- 没有错误,但输出不正确.可能存在指针问题