gpt4 book ai didi

visual-studio-2010 - std::make_pair 的这种使用在 GCC 3 和 4 中运行良好。它在 Visual Studio C++ 2010 中失败。为什么?

转载 作者:行者123 更新时间:2023-12-02 10:41:42 26 4
gpt4 key购买 nike

这在 GCC 3 和 4 中编译得很好。MSVC++ 无法确定 noFunction 的类型并抛出一些可怕的错误。请注意,如果您将 noFunction 转换为 BFunction,它在 VS2010 中工作得很好。

我的问题:这是 VS2010 中的缺陷,还是 GCC 违反规则?

#include <map>

using namespace std;

typedef bool (*AFunction)(int arg1, int arg2);
typedef bool (*BFunction)(long arg1, bool arg2);

bool noFunction(long, bool) { return true; }

void test(AFunction a)
{

make_pair(a, noFunction); //fails in VS2010

}

注:将 noFunction 转换为 BFunction 解决了 VS2010 中的问题。
make_pair(a, (BFunction)noFunction);  //works everywhere

.

.

.

以下是错误供引用:
    1>  makepairtest.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\type_traits(197): error C2752: 'std::tr1::_Remove_reference<_Ty>' : more than one partial specialization matches the template argument list
1> with
1> [
1> _Ty=bool (__cdecl &)(long,bool)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xtr1common(356): could be 'std::tr1::_Remove_reference<_Ty&&>'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xtr1common(350): or 'std::tr1::_Remove_reference<_Ty&>'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\type_traits(962) : see reference to class template instantiation 'std::tr1::remove_reference<_Ty>' being compiled
1> with
1> [
1> _Ty=bool (__cdecl &)(long,bool)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(26) : see reference to class template instantiation 'std::tr1::decay<_Ty>' being compiled
1> with
1> [
1> _Ty=bool (__cdecl &)(long,bool)
1> ]
1> c:\xxx\makepairtest.cpp(14) : see reference to class template instantiation 'std::tr1::_Unrefwrap<_Type>' being compiled
1> with
1> [
1> _Type=bool (__cdecl &)(long,bool)
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\type_traits(965): error C2528: 'abstract declarator' : pointer to reference is illegal
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\type_traits(349): error C2528: 'type' : pointer to reference is illegal
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\type_traits(967) : see reference to class template instantiation 'std::tr1::add_pointer<_Ty>' being compiled
1> with
1> [
1> _Ty=bool (__cdecl &)(long,bool)
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(148): error C2535: 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base(const _Ty1 &,const _Ty2)' : member function already defined or declared
1> with
1> [
1> _Ty1=bool (__cdecl *)(int,int),
1> _Ty2=bool (__cdecl &)(long,bool)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(134) : see declaration of 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base'
1> with
1> [
1> _Ty1=bool (__cdecl *)(int,int),
1> _Ty2=bool (__cdecl &)(long,bool)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(174) : see reference to class template instantiation 'std::_Pair_base<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=bool (__cdecl *)(int,int),
1> _Ty2=bool (__cdecl &)(long,bool)
1> ]
1> c:\xxx\makepairtest.cpp(14) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1> _Ty1=bool (__cdecl *)(int,int),
1> _Ty2=bool (__cdecl &)(long,bool)
1> ]

最佳答案

取地址noFunction适用于 VC10 和 gcc 4.5.2,即:

make_pair(a, &noFunction);

根据您发布的错误消息,我猜这与 VC 如何处理绑定(bind)到右值有关。

关于visual-studio-2010 - std::make_pair 的这种使用在 GCC 3 和 4 中运行良好。它在 Visual Studio C++ 2010 中失败。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459072/

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