[英]Why cant cast from pointer to reference to pointer in template function
我在編譯時出錯
INLINE template<typename T> T *&Animation::GetKey(int subAnim, int node, int frameInSubAnim)
{
const int keyIndex = GetKeyIndex(subAnim, node, frameInSubAnim);
return static_cast<T*>(m_Keys[keyIndex]);
}
有以下錯誤
d:\before_me\motion\pipeline\animation\AnimationData.inl(98):
error C2440: 'return' : cannot convert from 'Motion::Animation::Key *' to 'Motion::Animation::Key *&'
以及如何解決呢?
編譯器告訴您, static_cast<T*>(...)
產生一個臨時值 ( rvalue ),並且不能由非常量引用綁定(返回類型為T*&
)。 請注意,即使它會綁定到T*const&
您也並不需要。
目前尚不清楚您要實現的目標,但可以考慮返回T*
(刪除引用)。
我覺得這個捕捉你想要什么,並提供了一個可怕的解決方法
void* m_keys[] = { 0, 0, 0 };
template<typename T>
T*& foo(const int index)
{
return *reinterpret_cast<T**>(&m_keys[index]);
}
int main()
{
foo<int>(0) = new int();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.