SFINAE 序列化共享指针失败
SFINAE failure serializing shared pointer
我正在尝试在 c++14 模式下使用 boost 1.58、gcc 5.4 将 boost 序列化模板专用化为指向具有特定基类的对象的共享指针。 我相信我有正确的 SFINAE 设置来专门化该函数,但编译器不会像我期望的那样选择该函数。
#include <memory>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
class Artifact {
public:
Artifact() {}
virtual ~Artifact() {} // make polymorphic
};
class TestArtifact: public Artifact {
public:
TestArtifact() : Artifact() {}
};
namespace boost {
namespace serialization {
template <class Archive, class T>
void serialize(Archive &ar,
std::shared_ptr<typename std::enable_if<std::is_base_of<Artifact,T>::value,T>::type>& artifact,
//std::shared_ptr<T>& artifact,
unsigned int version) {
// save common Artifact structure on save ...
// Use factory to create specific Artifact (like TestArtifact) shared pointer
// from common Artifact info on load
}
}
}
int main(int argc, char** argv) {
auto t = std::make_shared<TestArtifact>();
std::cout << typeid(std::shared_ptr<TestArtifact>).name() << std::endl;
std::cout << typeid(std::shared_ptr<typename std::enable_if<std::is_base_of<Artifact,TestArtifact>::value,TestArtifact>::type>).name() << std::endl;
std::ofstream ofs(argv[1]);
boost::archive::binary_oarchive ar(ofs);
ar << t;
}
当我构建这段代码时(使用 g++ -std=c++14 -o test_template test_template.cpp -lboost_system -lboost_serialization),我收到以下错误(在正常的"必需自件"列表之后),
/usr/include/boost/serialization/access.hpp:118:9: error: ‘class std::shared_ptr<TestArtifact>’ has no member named ‘serialize’
t.serialize(ar, file_version);
这告诉我我没有正确设置SFINAE。 当我交换注释掉的行以专门用于std::shared_ptr<T>
时,代码按预期构建和运行,但我不想踩踏标准的 boost 共享指针序列化专用化。 当我使用过于通用的专业化运行并打印std::shared_ptr<TestArtifact>
的类型 ID 和 SFINAE 类输出时,它们是相同的(使用此编译器,我得到的输出为St10shared_ptrI12TestArtifactE
两者都如此),所以我不明白为什么我的 SFINAE 序列化函数没有得到识别。
问题是T
在这里无法推断。
不要将std::enable_if
放在函数参数中,而是将其放在单独的模板参数中。
template <class Archive, class T, typename std::enable_if<std::is_base_of<Artifact,T>::value>::type* = nullptr>
void serialize(Archive &ar, std::shared_ptr<T>& artifact, unsigned int version) { ... }
相关文章:
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- 函数返回时,带指针的复制构造函数失败
- 作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
- 模板参数推导/替换失败,lambda作为函数指针
- C++ 调试断言失败,无效的空指针
- 对于实际指针类型,用于检测类似指针(可取消引用)类型的模板函数失败
- C++指向成员的指针的类内初始化会使 MSVC 失败(但 GCC/Clang 工作)
- SFINAE 序列化共享指针失败
- 如何在类中存储指向字符串到成员函数映射的智能指针,以便它不会因类型不完整而失败?
- 通过功能指针通过DLL进行回调功能,运行时检查失败#0错误
- 为什么指针到成员函数的模板参数推断失败
- Visual Studio 2010 调用 _locking() 失败 (errno = 22) 当文件指针高于 (2^
- C++ 调试断言失败指针
- 删除SDL_VideoInfo指针时调试断言失败
- 使用通搭指针时在给定位置执行失败的原因,而不是更早?
- SetFilePointer不会失败,但也不会移动指针
- 返回指向指针数组的指针获取下一个元素失败
- 尝试将值分配给*此指针失败
- 模板化的出列无效指针:删除类时失败