gpt4 book ai didi

c++ - 64 位中指针和整数的模板重载问题

转载 作者:行者123 更新时间:2023-11-28 05:59:33 25 4
gpt4 key购买 nike

我的模板和重载不匹配的指针作为整数有问题。

C:/Users/Bakaiya/Desktop/Onathe/src/AdminPlanetRepresentation.cpp:24:66: error: cast from 'AdminPlanet*' to 'int' loses precision [-fpermissive] U::log( U::c( " + Creating planet representation ", 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.

最佳答案

根据链接代码,问题是 U::log( U::c( "+ Creating planet representation ", int( target ) ) ) 中的 int( target ); 并且与此处显示的代码无关。

改用uintptr_t(target)

关于c++ - 64 位中指针和整数的模板重载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33546302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com