问题描述
我正在尝试为实现成员函数的特定类型覆盖C ++模板。在我看来,当前的语言规范无法做到这一点。
如果我对一种特定类型使用专门化,则覆盖仅适用。
代码始终打印World而不是Hello。
有什么办法告诉编译器使用其他模板吗?
我无法修改serialize()的第一个模板,因为它是外部库的一部分。我可以添加其他模板。
#include <iostream>
#include <type_traits>
#include <new>
struct PanelBarcode
{
std::string value;
template<class S> void Serialize(S& in_s)
{
std::cout << value;
}
};
template <typename Archive,typename C>
struct has_serialize
{
private:
template<typename T>
static constexpr auto check(T*)
-> typename
std::is_same<
decltype(std::declval<T>().Serialize(std::declval<Archive&>())),void
>::type;
template<typename>
static constexpr std::false_type check(...);
typedef decltype(check<C>(0)) type;
public:
static constexpr bool value = type::value;
};
struct X {
X() {}
template<class S> void Serialize(S& in_s)
{ }
};
template<class A,class T>
void serialize(A ar,T& obj,const unsigned int version)
{
std::cout << "World";
}
template<class A,class T,typename std::enable_if_t<has_serialize<A,T>::value> * = nullptr>
void serialize(A ar,const unsigned int version)
{
obj.Serialize(ar);
}
struct Y
{
Y() {}
};
int main()
{
PanelBarcode bc;
bc.value = "Hello";
Y y;
serialize<Y,PanelBarcode>(y,bc,13);
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)