gpt4 book ai didi

templates - 无法从 FieldType* 推断出 T* 的模板参数(仅限 Visual C++)

转载 作者:行者123 更新时间:2023-12-04 15:40:12 24 4
gpt4 key购买 nike

此代码在 上编译成功g++ ( Coliru ) ,但不是 Visual C++ ( rextester ) - 在线和我的桌面。

它是一个更大的 Visual Studio 2015 项目的简化版本。

class AAA{
public: template<class T> static T* test(T* hqi){
return hqi;
}
};
class TTT3{
public: int d; //In real case, it is some class, but same error nonetheless.
decltype(AAA::test(&d)) dfd=AAA::test(&d); //<-- error only Visual C++
};
int main(){
int b;
decltype(AAA::test(&b)) dfd=AAA::test(&b); //OK for boths
}

'T *AAA::test(T *)': could not deduce template argument for 'T ' from 'int TTT3:: '



问题
  • 为什么?我的代码错了吗? - 我不这么认为。
  • 如何使它在 Visual C++ 中编译?我需要它。
  • 最佳答案

    这是 Visual Studio 特定的错误。根据标准:

    [expr.unary.op/4]

    A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [ Note: That is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type “pointer to member”. Neither does qualified-id, because there is no implicit conversion from a qualified-id for a non-static member function to the type “pointer to member function” as there is from an lvalue of function type to the type “pointer to function” ([conv.func]). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id's class. — end note ]



    粗体文本是 VC++ 在 decltype 中由于某种原因没有正确执行的操作。由于希望 Microsoft 修复它是愚蠢的希望,因此您可以做的另一种解决方法是添加以下重载:
    template<class C, typename T>
    static T* test(T C::*);

    Live Example

    可能在 #ifdef/#endif检查 VC++ 的 block 。不定义它可以防止它在未评估的上下文(例如 decltype)之外被静默选择,尽管只有链接时间错误。

    关于templates - 无法从 FieldType* 推断出 T* 的模板参数(仅限 Visual C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706852/

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