【发布时间】:2021-05-03 14:44:24
【问题描述】:
我在尝试使用特定编译器版本进行编译时遇到编译器错误。
IE。 icc 17.0 与-std=c++17 -O3
编译器错误:
source>(19): error: no suitable user-defined conversion from "Data" to "std::__cxx11::string" exists
Data temp{std::forward<Data>(d)};
^
compilation aborted for <source> (code 2)
ASM generation compiler returned: 2
<source>(19): error: no suitable user-defined conversion from "Data" to "std::__cxx11::string" exists
Data temp{std::forward<Data>(d)};
^
compilation aborted for <source> (code 2)
Execution build compiler returned: 2
代码:
#include <string>
#include <vector>
#include <iostream>
struct Data {
std::string id{};
std::string rowData{};
int totalRawDataLength{};
std::vector<int> rawDataOffset{};
std::vector<int> rawDataLength{};
Data() = default;
Data(const Data&d) =default;
Data(Data &&d) =default;
};
Data ProcessData(Data &&d) {
Data temp{std::forward<Data>(d)};
// some code
return temp;
}
int main() {
Data d{};
d.id = "id_001";
d.rowData = "some data";
d.rawDataOffset.emplace_back(4);
d.rawDataLength.emplace_back(4);
auto x = ProcessData(std::move(d));
std::cout << "Test:" << x.id << std::endl;
return 0;
}
以下代码适用于所有版本的gcc,并且它适用于具有相同编译器选项的更高版本的 icc。
它甚至适用于 icc 17.0 的 -std=c++11 -O3
在进一步调试中发现正在生成的默认复制构造函数有问题。
我无法理解发生了什么问题,听说这是某种编译器错误,在以后的版本中得到了解决?
【问题讨论】:
-
如果将
#include <utility>添加到标题列表中会发生什么? -
@dfrib nope 我没有尝试为不同 ABI 编译的链接库
-
@AdrianMole 得到同样的错误
-
这似乎是在应用聚合初始化,而不是调用构造函数。您可以尝试
Data temp(std::forward<Data>(d));(使用括号而不是大括号)和/或用Data() { }替换默认构造函数来解决此问题。 -
@1201ProgramAlarm 您的建议解决了编译问题,即使用
Data(const Data&d) {}替换默认复制构造函数也解决了编译问题。你能帮我理解发生了什么问题以及为什么在其他版本或不同的 c++ 标准中这不是问题