1

现在我将我的项目从Visual Studio 2008迁移到2013(未安装更新),并且面临问题。 我有一种Variant类型,CData< T>,具有转换运营商所包含的类型T在VS2008中编译的代码不在VS2013中,const重载

template<class T> T&  GetValue(); 
template<class T> const T& GetValue() const; 
template<class T> T*  GetValuePtr(); 
template<class T> const T* GetValuePtr() const; 

template<class T>   operator T&() { return GetValue<T>(); } 
template<class T>   operator const T&() const { return GetValue<T>(); } 
template<class T>   operator T*() { return GetValuePtr<T>(); } 
template<class T>   operator const T*() const { return GetValuePtr<T>(); } 

有一类CDataArray方法:

CData&   Get(int Index) { return At(Index); } 
const CData& Get(int Index) const { return operator [](Index); } 
template<class T> 
T&    Get(int Index) { return At(Index).GetValue<T>(); } 
template<class T> 
const T&  Get(int Index) const { return operator [](Index).GetValue<T>(); } 

,有一种叫它:

Data::PDataArray SGQuests; 
Data::PParams SGQuest = SGQuests->Get(i); 

这里,数据:: PParams将是T. 在VS2008中,似乎它使用非const非模板CDataArray ::获取(),W返回CData &,然后使用非const模板运算符T &()访问此CData &,最后返回Data :: PParams &。

在VS2013中,由于某种原因,使用一个const超载,它会导致错误:

1>PATH_TO_SRC\l1\data\type.h(126): error C2678: binary "=": no operator found that accepts left operand "const Ptr<Data::CParams>" (or there is no acceptable conversion) 
1>   PATH_TO_SRC\l1\data\ptr.h(29): may be "void Ptr<Data::CParams>::operator =(T *)" 
1>   with 
1>   [ 
1>    T=Data::CParams 
1>   ] 
1>   PATH_TO_SRC\l1\data\ptr.h(28): or  "void Ptr<Data::CParams>::operator =(const Ptr<Data::CParams> &)" 
1>   trying to match argument list "(const Ptr<Data::CParams>, const Ptr<Data::CParams>)" 
1>   PATH_TO_SRC\l1\data\type.h(125): compiling member function "void Data::CTypeImpl<T>::Copy(void **,void *const *) const" template class 
1>   with 
1>   [ 
1>    T=const Ptr<Data::CParams> 
1>   ] 
1>   PATH_TO_SRC\l1\data\data.h(167): "Data::CTypeImpl<T>" 
1>   with 
1>   [ 
1>    T=const Ptr<Data::CParams> 
1>   ] 
1>   PATH_TO_SRC\l1\data\data.h(97): "T &Data::CData::GetValue<T>(void)" 
1>   with 
1>   [ 
1>    T=const Ptr<Data::CParams> 
1>   ] 
1>   PATH_TO_SRC\l3\quests\questmanager.cpp(307): "Data::CData::operator const T(void)<const Ptr<Data::CParams>>" 
1>   with 
1>   [ 
1>    T=const Ptr<Data::CParams> 
1>   ] 

我手动翻译的消息,以英语,让他们可以稍微从原来的EN编译器的消息不同。

最后,如果我写的显式模板参数:

Data::PParams SGQuest = SGQuests->Get<Data::PParams>(i); 

它编译OK。

的问题是:

  1. 如何VS2013模板参数猜测从VS2008一个不同?有没有什么地方(标准,文章或水平)明确解释,为什么我的旧代码不应该编译?任何参考将不胜感激。
  2. 我应该如何在这种情况下编写代码?我是否必须现在显式编写模板参数,或者只是修改成员重载,或者安装一些更新?

P.S.最小的代码是https://www.dropbox.com/s/zjohnu5v87tyr2c/ConstOverload.zip?dl=0。完整的源代码在 https://code.google.com/p/deusexmachina/source/browse/branches/Dev/DEM/Src

+3

而不是给我们一个链接到您的整个代码库,你可以减少所有这些代码缩减到几个虚拟类,只有几个函数产生的错误?此外,名称查询是一个棘手的业务,再加上您使用的铸造操作员是另一个复杂程度。即使可编译的2008版本可能会调用我不期望的代码,我也会紧张。 – PaulMcKenzie 2014-09-06 01:25:29

+0

在你最小的例子中,你缺少'SimpleString.h'和'Hashtable.h'。但是,为什么当'std :: string'时需要另一个字符串类,为什么现在有'std :: unordered_map'的时候需要一个哈希表? – PaulMcKenzie 2014-09-07 22:09:01

+0

我不打算让这个例子运行,因为一个问题是编译时。试着用Ctrl + F7编译Main.cpp,你会看到一个错误。为什么我比STL更喜欢我自己的课是一个偏离主题,而且,它真的值得讨论吗? – Niello 2014-09-08 09:56:45

回答

相关问题