문제

다음 코드는 Visual C ++ 및 GCC에서 컴파일하지만 Code Warrior에서는 실패합니다.

불만은 템플릿에 대한 호출이 모호하다는 것입니다. 각 경우에 따라 DOIT (m*)와 doit (m const*) 사이를 결정할 수 없다는 것입니다. 두 번째 템플릿 인수를 제공하면 더 이상 모호하지 않다고 결정합니다.

template< typename T1, typename T2 >
T1 const* doIt( T2 const* );

template< typename T1, typename T2 >
T1* doIt( T2* );

class M {};
class N : public M {};

void f()
{
   M* m1 = NULL;
   M const* m2 = NULL;

   doIt<N>( m1 );    // Fail
   doIt<N>( m2 );    // Fail
   doIt<N,M>( m1 );  // OK
   doIt<N,M>( m2 );  // OK

}

코드 전사 컴파일러의 오류 일뿐입니까? (또는 GCC/Visual C ++의 오류).

도움이 되었습니까?

해결책

CodeWarrior 컴파일러의 오류입니다.

이것이 일어나야 할 일입니다.

template< typename T1, typename T2 >
T1 const* doIt( T2 const* );   // 1

template< typename T1, typename T2 >
T1* doIt( T2* );  // 2

class M {};
class N : public M {};

void f()
{
  M* m1 = 0;
  M const* m2 = 0;

  doIt<N>( m1 );    
  // In the above call - the compiler does the following (post argument deduction)
  //  1) create a viable set of functions  { N* doIt1<N,M>(const M*) , N* doIt2<N, M>(M*) }
  //  2) check the conversion sequences - M* -> M* is better than M* -> const M*
  //  Since doIt2 has a "better" conversion sequence (hard to beat identity) it wins - no ambiguity


  doIt<N>( m2 );    
  // 1) Viable functions: { doIt1<N,M>(const M*), doIt2<N,const M>(const M*) }
  // 2) Conversion Sequence Ranking: both do identity - so both are good
  // 3) Check to see if the "mother" template of either candidate is more specialized
  //     - Since doIt1 theoretically matches fewer types than doIt2, it is unambiguously more specialized (the standard specifies an algorithm to check this)
  //     - so doIt1 wins
}

도움이되기를 바랍니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top