【发布时间】:2023-03-05 09:40:02
【问题描述】:
我的模板有问题,并将不匹配的指针重载为整数。
C:/Users/Bakaiya/Desktop/Onathe/src/AdminPlanetRepresentation.cpp:24:66: 错误:从 'AdminPlanet*' 转换为 'int' 失去精度 [-fpermissive] U::log( U::c( " + 创建行星表示 ", int( target ) ) );
我对“c()”模板有几个重载,但我当前的问题围绕着为什么我最近添加的指针重载没有解决这个问题。
template<class T>
inline String c( T a )
{
return Ogre::StringConverter::toString( a );
}
template<class T>
inline String c( T* a )
{
return c( (uint64_t)a );
}
还有多参数模板:
template<class T, typename... Args>
inline String c( T a, Args... args );
template<class T, typename... Args>
inline String c( T* a, Args... args );
...
template<class T, typename... Args>
inline String c( T a, Args... args )
{
return Ogre::StringConverter::toString( a ) + c( args... );
}
template<class T, typename... Args>
inline String c( T* a, Args... args )
{
return c( a ) + c( args... );
}
以前我构建为 32 位,让指针隐式转换为通用模板 c( T a ) 没有问题。但是,在 64 位版本上,这还不够。
为什么重载没有捕获指针并阻止它们被隐式转换为 int?
包含其他重载和模板的完整文件是available here.
【问题讨论】:
-
使用
T const* a效果更好吗? -
@Jarod42 更正了术语。