컴파일러 오류 C2666

'identifier': 숫자 오버로드의 변환이 유사합니다.

오버로드된 함수 또는 연산자가 모호합니다. 형식 매개 변수 목록은 컴파일러가 모호성을 해결하기에는 너무 비슷할 수 있습니다. 이 오류를 해결하려면 하나 이상의 실제 매개 변수를 명시적으로 캐스팅합니다.

예제

다음 샘플에서는 C2666을 생성합니다.

// C2666.cpp
struct complex {
   complex(double);
};

void h(int,complex);
void h(double, double);

int main() {
   h(3,4);   // C2666
}

이 오류는 Visual Studio 2019 버전 16.1에 대해 수행된 컴파일러 규칙 작업의 결과로 생성될 수 있습니다.

  • 기본 형식이 기본 형식으로 고정되는 열거형의 수준을 올리는 변환이 두 형식이 다른 경우 승격된 기본 형식으로 수준을 올리는 변환보다 좋습니다.

다음 예제에서는 Visual Studio 2019 버전 16.1 이상 버전에서 컴파일러 동작이 어떻게 변경되는지 보여 줍니다.

#include <type_traits>

enum E : unsigned char { e };

int f(unsigned int)
{
    return 1;
}

int f(unsigned char)
{
    return 2;
}

struct A {};
struct B : public A {};

int f(unsigned int, const B&)
{
    return 3;
}

int f(unsigned char, const A&)
{
    return 4;
}

int main()
{
    // Calls f(unsigned char) in 16.1 and later. Called f(unsigned int) in earlier versions.
    // The conversion from 'E' to the fixed underlying type 'unsigned char' is better than the
    // conversion from 'E' to the promoted type 'unsigned int'.
    f(e);
  
    // Error C2666. This call is ambiguous, but previously called f(unsigned int, const B&). 
    f(e, B{});
}

Visual Studio .NET 2003에 대해 수행된 컴파일러 규칙 작업의 결과로 이 오류를 생성할 수도 있습니다.

  • 포인터 형식으로의 이진 연산자 및 사용자 정의 변환

  • 정규화 변환은 ID 변환과 동일하지 않습니다.

매개 변수의 형식이 피연산자의 형식으로 변환할 사용자 정의 변환 연산자를 정의하는 경우 이진 연산<자 , ><= 및 >=의 경우 전달된 매개 변수는 이제 피연산자의 형식으로 암시적으로 변환됩니다. 이제 모호할 가능성이 있습니다.

Visual Studio .NET 2003 및 Visual Studio .NET 버전의 Visual C++에서 모두 유효한 코드의 경우 함수 구문을 사용하여 클래스 연산자를 명시적으로 호출합니다.

// C2666b.cpp
#include <string.h>
#include <stdio.h>

struct T
{
    T( const T& copy )
    {
        m_str = copy.m_str;
    }

    T( const char* str )
    {
        int iSize = (strlen( str )+ 1);
        m_str = new char[ iSize ];
        if (m_str)
            strcpy_s( m_str, iSize, str );
    }

    bool operator<( const T& RHS )
    {
        return m_str < RHS.m_str;
    }

    operator char*() const
    {
        return m_str;
    }

    char* m_str;
};

int main()
{
    T str1( "ABCD" );
    const char* str2 = "DEFG";

    // Error - Ambiguous call to operator<()
    // Trying to convert str1 to char* and then call
    // operator<( const char*, const char* )?
    //  OR
    // trying to convert str2 to T and then call
    // T::operator<( const T& )?

    if( str1 < str2 )   // C2666

    if ( str1.operator < ( str2 ) )   // Treat str2 as type T
        printf_s("str1.operator < ( str2 )\n");

    if ( str1.operator char*() < str2 )   // Treat str1 as type char*
        printf_s("str1.operator char*() < str2\n");
}

다음 샘플에서는 C2666을 생성합니다.

// C2666c.cpp
// compile with: /c

enum E
{
    E_A,   E_B
};

class A
{
    int h(const E e) const {return 0; }
    int h(const int i) { return 1; }
    // Uncomment the following line to resolve.
    // int h(const E e) { return 0; }

    void Test()
    {
        h(E_A);   // C2666
        h((const int) E_A);
        h((int) E_A);
    }
};