C ++ Cannot resolve overloaded function template

First, I don't understand why the compiler can't solve the code breaking issue (wrapped by the ENABLE_OFFENDER macro). The two get () methods have very different call signatures. So, perhaps a C ++ lawyer can help explain why I am getting the error.

Second, is there a way to provide more instructions to the compiler as to which get () it should use?

#include <iostream>

//switch offending code in/out
#define ENABLE_OFFENDER

/// Forward declare
template <typename T> class TableEntry;

/// Non-template base class
class TableEntryBase {
protected:
    // prevent instantiation
    TableEntryBase() { }
public:
    virtual ~TableEntryBase() { }

    template <typename T>
    void set(const T& rvalue){
        TableEntry<T>* entry = dynamic_cast<TableEntry<T> *>(this);
        if(0 != entry){
            entry->setValue(rvalue);
        }
    }

    template <typename T>
    T get(T& lvalue){
        TableEntry<T>* entry = dynamic_cast<TableEntry<T> *>(this);
        if(0 != entry){
            return entry->getValue(lvalue);
        } else {
            return T();
        }
    }

    template <typename T>
    T get(){
        TableEntry<T>* entry = dynamic_cast<TableEntry<T> *>(this);
        if(0 != entry){
            return entry->getValue();
        } else {
            return T();
        }
    }

};

template <typename T>
class TableEntry : public TableEntryBase {
private:
    T m_value;
public:
    TableEntry():TableEntryBase() { }
    ~TableEntry() { }
    void setValue(const T& rvalue){
        m_value = rvalue;
    }
    T getValue(T& lvalue){
        lvalue = m_value;
        return m_value;
    }
    T getValue(){
        return m_value;
    }
};


template <int N>
class Table {
private:
    TableEntryBase* m_tableEntries[N];
    int m_tableEntriesIndex;
public:
    Table():m_tableEntriesIndex(0){}

    virtual ~Table() { }

    void addEntry(TableEntryBase* entry){
        if(0 != entry)
            m_tableEntries[m_tableEntriesIndex++] = entry;
    }

    template <typename T>
    void setEntry(int entryIndex, const T& rvalue){
        // I'm not sure why it not set<T>(rvalue)
        m_tableEntries[entryIndex]->set(rvalue);
    }

    template <typename T>
    T getEntry(int entryIndex, T& lvalue){
        return m_tableEntries[entryIndex]->get(lvalue);
    }

#ifdef ENABLE_OFFENDER
    template <typename T>
    T getEntry(int entryIndex){
        return m_tableEntries[entryIndex]->get();
    }
#endif
};


int main(){
    TableEntry<int> entry1;
    // setting the value using base class set
    entry1.set<int>(5);

    TableEntry<double> entry2;
    entry2.set<double>(3.14);

    std::cout << "entry1 value = " << entry1.getValue() << std::endl;
    std::cout << "entry2 value = " << entry2.getValue() << std::endl;

    std::cout << "entry1 value = " << entry1.get<int>() << std::endl;
    std::cout << "entry2 value = " << entry2.get<double>() << std::endl;
    TableEntryBase* entry_ptr = &entry1;
    std::cout << "entry1 value = " << entry_ptr->get<int>() << std::endl;

    Table<2> table;
    table.addEntry(&entry1);
    table.addEntry(&entry2);
    table.setEntry<int>(0, 10);
    std::cout << "entry1 value = " << entry1.get<int>() << std::endl;
    std::cout << "entry2 value = " << entry2.get<double>() << std::endl;
    int val3 = 0;
    int val4 = 0;
    val4 = table.getEntry<int>(0, val3);
    int val5 = 0;
#ifdef ENABLE_OFFENDER
    val5 = table.getEntry<int>(0);
#endif
    std::cout << "val3 = " << val3 << ", val4 = " << val4 << ", val5 = " << val5 << std::endl;
    return 0;
}

/* GCC error
main.cpp:129:30:   instantiated from here
main.cpp:96:95: error: no matching function for call to โ€˜TableEntryBase::get()โ€™
main.cpp:96:95: note: candidates are:
main.cpp:26:4: note: template<class T> T TableEntryBase::get(T&)
main.cpp:36:4: note: template<class T> T TableEntryBase::get()
*/

      

I got the code to work with my old compilers by modifying the offending code as such.

#ifdef ENABLE_OFFENDER
    template <typename T>
    T getEntry(int entryIndex){
        // Error: expected primary-expression before โ€˜>โ€™ token
        //return m_tableEntries[entryIndex]->get<T>();

        // Error: couldn't deduce template parameter โ€˜Tโ€™
        //return m_tableEntries[entryIndex]->get();

        TableEntryBase* entry = m_tableEntries[entryIndex];
        return entry->get<T>();
    }
#endif

      

+3


source to share





All Articles