开发者

Member function overloading/template specialization issue

开发者 https://www.devze.com 2023-02-02 22:04 出处:网络
I\'ve been trying to call the overloaded table::scan_index(std::string, ...) member function without success. For the sake of clarity, I have stripped out all non-relevant code.

I've been trying to call the overloaded table::scan_index(std::string, ...) member function without success. For the sake of clarity, I have stripped out all non-relevant code.

I have a class called table which has an overloaded/templated member function named scan_index() in order to handle strings as a special case.

class table : boost::noncopyable
{
public:
    template <typename T>
    void scan_index(T val, std::function<bool (uint recno, T val)> callback) {
        // code
    }

    void scan_index(std::string val, std::function<bool (uint recno, std::string val)> callback) {
        // code
    }
};

Then there is a hitlist class which has a number of templated member functions which call table::scan_index(T, ...)

class hitlist {
public:
    template <typename T>
    void eq(uint fieldno, T value) {
        table* index_table = db.get_index_table(fieldno);
        // code
        index_table->scan_index<T>(value, [&](uint recno, T n)->bool {
            // code
        });
    }
};

And, finally, the code which kicks it all off:

hitlist hl;
// code
hl.eq<std::string>(*fieldno, p1.to_string());

The problem is that instead of calling table::scan_index(std::string, ...), it calls the templated version. I have tried using both overloading (as shown above) and a specialized function template (below), but nothing seems to work. After staring at th开发者_开发技巧is code for a few hours, I feel like I'm missing something obvious. Any ideas?

    template <>
    void scan_index<std::string>(std::string val, std::function<bool (uint recno, std::string val)> callback) {
        // code
    }

Update: I dropped the <T> decoration from the scan_index() call. The result was that the calls with a string parameter compile just fine, but calls with other types (e.g. double) resulted in the following error:

cannot convert parameter 1 from 'double' to 'std::string'

So I went back to using template specialization. Now I get this error:

error C2784: 'void table::scan_index(T,std::tr1::function<bool(uint,T)>)' :
  could not deduce template argument for 'std::tr1::function<bool(uint,T)>'
  from '`anonymous-namespace'::<lambda5>'

FYI: I am using VC++ 10.0

Solution: I fixed this problem by dropping the templated scan_index() function from the table class and simply writing four overloaded functions (three of which are identical except for the signature). Luckily, they're all pretty short (less than ten lines) so it's not so bad.


You explicitly call templated member here:

index_table->scan_index<T>(value, [&](uint recno, T n)...

Since value is the template parameter you should be fine replacing that with:

index_table->scan_index(value, [&](uint recno, T n)...


You shouldn't explicitly specify the template argument- you should leave it to template argument deduction and overload resolution. This should allow the overload you posted in the very original code to be picked up and used.


prepared this simplified test (sorry, I don't have C++0x compiler atm):

class table
{
public:
    template <typename T>
    void scan_index(T val) {
        std::cout << "template\n";
    }

    template <>
    void scan_index<int>(int val) {
        std::cout << "specialization\n";
    }

    void scan_index(int val) {
        std::cout << "overloaded\n";
    }
};

class hitlist {
public:
    template <typename T>
    void eq(T value) {
        table tbl;
        tbl.scan_index<T>(value);
    }
};

int main(int argc, char* argv[])
{
    int i = 0;
    hitlist hl;
    hl.eq(i);
    return 0;
}

output is "specialization" (VC9). it's because hitlist::eq explicitly uses template version, so only template specialization will be considered. if you change tbl.scan_index<T>(value); to tbl.scan_index(value); overloaded version will be called.

tbh I don't know why your template specialization doesn't work, probably because of lambda, not complete C++0x support? which compiler?


Isn't the template specialisation be something like:

template <typename T>
void scan_index(T val, std::function<bool (uint recno, T val)> callback) {
    // code
}
template <>
void scan_index(std::string val, std::function<bool (uint recno, std::string val)> callback) {
    // code
}

This way C++ compiler recognizes scan_index has a specialisation rather than looking for the overloaded method. And I believe if you wanted it the way you can coded, you should probably put the specialised code before the template like this:

    void scan_index(std::string val, std::function<bool (uint recno, std::string val)> callback) {
        // code
    }
template <typename T>
    void scan_index(T val, std::function<bool (uint recno, T val)> callback) {
        // code
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消