C++ `decltype`:函数返回类型'T'已转换为'T&;`对于用户定义类型(VS2010)

C++ `decltype`:函数返回类型'T'已转换为'T&;`对于用户定义类型(VS2010),c++,templates,return-type,decltype,C++,Templates,Return Type,Decltype,编辑:GCC编译得很好,是VS2010版。谢谢你给我指点ideone.com 在尝试编译以下内容(VS2010)时: #包括 模板 内联自动FCall01(PF01 fCallee、PF02和fParameter00)->数据类型(fCallee(标准:正向(fParameter00))) { decltype(fCallee(std::forward(fParameter00)))lResult(fCallee(std::forward(fParameter00));//Foo03的违规行 返

编辑:GCC编译得很好,是VS2010版。谢谢你给我指点ideone.com

在尝试编译以下内容(VS2010)时:

#包括
模板
内联自动FCall01(PF01 fCallee、PF02和fParameter00)->数据类型(fCallee(标准:正向(fParameter00)))
{
decltype(fCallee(std::forward(fParameter00)))lResult(fCallee(std::forward(fParameter00));//Foo03的违规行
返回(lResult);
}
int-gI=0;
int&gCI=gI;
结构TA
{
int-mData;
TA(int-fData=0):mData(fData){}
TA(TA常数和fA):mData(fA.mData){}
};
int Foo00(int&fA){return(fA);}
int&Foo01(int&){return(gCI);}
int const&Foo02(int&){return(gCI);}
tafoo03(int&fA){return(TA(fA));}
内部主(空)
{
decltype(FCall01(Foo00,gI))l0(FCall01(Foo00,gI));
l0=-1;

STD::CUT< P>这是VS2010中的编译器错误。它既不发生在GCC(IDENo.com)中,也不发生在Visual C++ 11开发者预览中。

感谢PrimHah和Xeo链接,杰西在VisualC++ 11开发者预览中对bug状态的信息。


编辑:只是一个跟进:当用户定义类型具有用户定义的构造函数时,VS2010中会出现此错误。如果它没有构造函数(使用编译器生成的默认值),则不会出现此错误。

提示:您可以随时尝试ideone.com(不是最新版本的)gccI尝试了codepad.org-它没有接受它。谢谢你的参考,去尝试它。每个人都可以访问GCC-。:)@Xeo是的,谢谢你们,伙计们,用于ideone.com链接。GCC编译它,必须是VS2010问题…我可以删除这个问题吗?现在似乎没用了。或者你们中的一个做了回答,我也会接受:)VS11开发者预览版编译得很好,所以似乎已经解决了这个问题。
#include <iostream>

template< typename PF01, typename PF02 >
inline auto FCall01( PF01 fCallee, PF02 && fParameter00 ) -> decltype( fCallee( std::forward< PF02 > ( fParameter00 ) ) )
{
 decltype( fCallee( std::forward< PF02 > ( fParameter00 ) ) ) lResult( fCallee( std::forward< PF02 >( fParameter00 ) ) ); // offending line for Foo03

 return ( lResult );
}

int gI = 0;
int & gCI = gI;

struct TA
{
  int mData;
  TA( int fData = 0 ) : mData( fData ) { }
  TA( TA const & fA ) : mData( fA.mData ) { }
};

int Foo00( int & fA ){ return ( fA ); }
int & Foo01( int & ){ return ( gCI ); }
int const & Foo02( int & ){ return ( gCI ); }
TA Foo03( int & fA ){ return ( TA( fA ) ); }

int main( void )
{
  decltype( FCall01( Foo00, gI ) ) l0( FCall01( Foo00, gI ) );
  l0 = -1;
  std::cout << gI << " : " << l0 << std::endl;

  decltype( FCall01( Foo01, gI ) ) l1( FCall01( Foo01, gI ) );
  l1 = -2;
  std::cout << gI << " : " << l1 << std::endl;

  decltype( FCall01( Foo02, gI ) ) l2( FCall01( Foo02, gI ) );
  const_cast< int & > ( l2 ) = -3;
  std::cout << gI << " : " << l2 << std::endl;

  decltype( FCall01( Foo03, gI ) ) l3( FCall01( Foo03, gI ) );
  l3.mData = -4;
  std::cout << gI << " : " << l3.mData << std::endl;

  return ( 0 );
}
Warning C4239: nonstandard extension used : 'initializing' : conversion from 'TA' to
'TA &'; A non-const reference may only be bound to an lvalue; see reference to function
template instantiation 'TA &FCall01<TA(__cdecl *)(int &),int&>(PF01,PF02) with
[PF01=TA(__cdecl *)(int &),PF02=int &]' being compiled.