Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member function overloading/template specialization issue

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 this 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.

like image 315
Ferruccio Avatar asked Dec 17 '22 18:12

Ferruccio


2 Answers

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)...
like image 111
Nikolai Fetissov Avatar answered Dec 19 '22 07:12

Nikolai Fetissov


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.

like image 42
Puppy Avatar answered Dec 19 '22 08:12

Puppy