简体   繁体   English

C ++对重载函数的不明确调用

[英]C++ Ambiguous call to overloaded function

I have the following code for a "safe" strncpy() -- basically it's wrapper automatically takes fixed array sizes for string buffers so you don't have to do the extra work to pass them in (and this convenience is safer because you won't accidentally type the wrong size for fixed array buffers). 我有以下代码用于“安全”strncpy() - 基本上它的包装器自动为字符串缓冲区采用固定的数组大小,因此你不必做额外的工作来传递它们(这样的便利是更安全的,因为你赢了不小心为固定数组缓冲区输入了错误的大小。

inline void MySafeStrncpy(char *strDest,size_t maxsize,const char *strSource)
{
    if(maxsize)
    {
        maxsize--;
        strncpy(strDest,strSource,maxsize);
        strDest[maxsize]=0;
    }
}

inline void MySafeStrncpy(char *strDest,size_t maxDestSize,
    const char *strSource, size_t maxSourceSize)
{
    size_t minSize=(maxDestSize<maxSourceSize) ? maxDestSize:maxSourceSize;
    MySafeStrncpy(strDest,minSize,strSource);
}

template <size_t size>
void MySafeStrncpy(char (&strDest)[size],const char *strSource)
{
    MySafeStrncpy(strDest,size,strSource);
}

template <size_t sizeDest,size_t sizeSource>
void MySafeStrncpy(char (&strDest)[sizeDest],
    const char (&strSource)[sizeSource])
{
    MySafeStrncpy(strDest,sizeDest,strSource,sizeSource);
}

template <size_t sizeSource>
void MySafeStrncpy(char *strDest,size_t maxDestSize,
    const char (&strSource)[sizeSource])
{
    MySafeStrncpy(strDest,maxDestSize,strSource,sizeSource);
}

Using the code results in an error in Visual C++ 2008 when compiling: 在编译时,使用代码会导致Visual C ++ 2008中出现错误:

char threadname[16];
MySafeStrncpy(threadname,"MainThread");

error C2668: 'MySafeStrncpy' : ambiguous call to overloaded function
>        could be 'void MySafeStrncpy<16,11>(char (&)[16],const char (&)[11])'
>        or       'void MySafeStrncpy<16>(char (&)[16],const char *)'
>        while trying to match the argument list '(char [16], const char [11])'

What am I doing wrong here? 我在这做错了什么?

It seems like the compiler is unable to determine if the character string literal "MainThread" should be treated as a const char * or a const char[11] when determining which template function to call. 在确定调用哪个模板函数时,似乎编译器无法确定字符串文字"MainThread"是否应被视为const char *const char[11]

I would like it to treat the string literal as a const char[11] and select the void MySafeStrncpy<16,11>(char (&)[16],const char (&)[11]) variant since that is the "safest". 我希望它将字符串文字视为const char[11]并选择void MySafeStrncpy<16,11>(char (&)[16],const char (&)[11])变体,因为那是“最安全的”。

Also two more constraints on answers: 1) I am unable to switch compilers (the code compiles on other compilers) and 2) the company will not allow me to use external template libraries for a solution. 另外还有两个答案限制:1)我无法切换编译器(代码编译在其他编译器上)和2)公司不允许我使用外部模板库来解决问题。

According to 13.3.3.1.1, Array-to-pointer conversion has Exact Match rank, so this function call might be ambiguous in the standard spec. 根据13.3.3.1.1, 数组到指针的转换具有完全匹配等级,因此该函数调用在标准规范中可能不明确。 If you are allowed to change the definition: 如果允许您更改定义:

template <size_t size>
void MySafeStrncpy(char (&strDest)[size],const char *strSource)

to: 至:

template <size_t size, class T>
void MySafeStrncpy(char (&strDest)[size], T strSource)

like here , then probably that'll be the simplest workaround. 就像这里一样,那可能是最简单的解决方法。

The overloads for char-array/const-char-array and char-array/const-char-pointer can't be distinguished from each other by overload resolution logic (at least the Microsoft logic - this code compiles just fine in GCC. Don't know that the standard says here.). char-array / const-char-array和char-array / const-char-pointer的重载不能通过重载解析逻辑相互区分(至少是Microsoft逻辑 - 这个代码在GCC中编译得很好.Don不知道标准在这里说的。) You need to distinguish them in some way, eg by renaming the function for arrays or adding a dummy parameter. 您需要以某种方式区分它们,例如通过重命名数组函数或添加虚拟参数。

An elegant solution would be using boost::enable_if and boost::is_array. 优雅的解决方案是使用boost :: enable_if和boost :: is_array。

When u are using your function : 当你使用你的功能时:

MySafeStrncpy(threadname,"MainThread");

you are not passing the size_t argument in between threadname & "MainThread", but u have defined in function definition. 你没有在threadname和“MainThread”之间传递size_t参数,但是你已经在函数定义中定义了。

it shud be something like this : 它就是这样的:

MySafeStrncpy(threadname, sizeof threadname, "MainThread");

if u dont want to pass the argument then make it default in ur function definition. 如果你不想传递参数,那么在ur函数定义中将其设为默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM