【发布时间】:2017-07-22 15:36:16
【问题描述】:
我有一个从文本文件加载的免费模板函数:
// free template function
template<class U>
bool Main_Load(U& dataset, const std::string path) {
// Create an input archive
std::ifstream ifs(path);
boost::archive::text_iarchive archive_text(ifs);
// Load data
archive_text & dataset;
}
...
// in main()
Dataset1 dataset_restored;
Main_Load(dataset_restored, filename); // <--- OK
但是当我从成员函数调用这个模板函数时,我得到一个错误“what(): input stream error”:
class Dataset1 {
...
virtual auto LoadDataset(AUX::DATA::SerializationType serialisation_type=AUX::DATA::SerializationType::Xml) -> bool {
Main_Load(*this, GetPath()); // <--- ERROR
};
};
...
// in main()
Dataset1 dataset_restored;
dataset_restored.LoadDataset(AUX::DATA::SerializationType::Text);
为什么我可以传递同一个对象的 dataset_restored 但不能传递 *this?我想与 *this 结合使用有问题:
archive_text & dataset;
编辑 我已经回答了我自己的问题。见下文。
【问题讨论】:
-
请提供minimal reproducible example 以重现问题。
标签: c++ templates pass-by-reference boost-serialization