简体   繁体   中英

Ambiguous overload of std::vector::operator[]

I have a simple wrapper around C null-terminated string, which is essentially a subclass of std::vector< char >. (Yes, I know about std::string, but my wrapper is easier to cooperate with C functions expecting char*. Also, std::string isn't guaranteed to be contiguous in C++03)

Here is the code:

#include <cstdio>
#include <vector>

typedef std::vector<char> vector_char;
class c_string : public vector_char
{
    public:
    c_string(size_t size) : vector_char(size+1) {}

    c_string(const char* str)
    {
        if(!str) return;
        const char* iter = str;
        do
            this->push_back(*iter);
        while(*iter++);
    }

    c_string() {}

    //c_string(std::nullptr_t) {}

    char* data()
    {
        if(this->size())
            return &((*this)[0]); //line 26
        else
            return 0; 
    }

    const char* data() const { return this->data(); }

    operator char*() { return this->data(); }
    operator const char*() const { return this->data(); }

};

int main()
{
    c_string first("Hello world");
    c_string second(1024);

    printf("%s",first.data());
    printf("%c\n",first[0]);
    snprintf(second, second.size(), "%d %d %d", 5353, 22, 777);
    printf(second);
}

MinGW complains about:

D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp: In member function 'char* c_string::data()':

D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp:26:22: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]

In file included from d:\prog\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/vector:65:0,
                 from D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp:2:
d:\prog\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/bits/stl_vector.h:768:7:note: candidate 1: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[]:(std::vector<_Tp, _Alloc>::size_type) [with _Tp = char; _Alloc = std:
:allocator<char>; std::vector<_Tp, _Alloc>::reference = char&; std::vector<_Tp,_Alloc>::size_type = unsigned int]


D:\prog\PROJEKTYCPP\hehe_testc_cpp.cpp:26:22: note: candidate 2: operator[](char
*, int) <built-in>

How can I enforce calling correct overload? Can this problem hurt me silently?

By having an operator char * you've provided two ways to do operator[] . The first is std::vector::operator[] applied directly; the second is to convert this to char* and apply [] to that. In this case they both result in the same thing, but the compiler can't know that.

Resolve it by specifying explicitly which one you want.

        return &(operator[](0)); //line 26

or

        return &((char*)(*this)[0]); //line 26

To delete the first warning, you can do this:

char* data()
{
    if(this->size())
        return &vector_char::operator[](0);
    else
        return 0;
}

To delete all warnings, remove the operator char*() and operator const char*() members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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