Overloaded template function

Discussion in 'C++' started by Dave Corby, Oct 12, 2005.

  1. Dave Corby

    Dave Corby Guest

    Hi all,

    I have an overloaded template function, and in one particular spot can't
    get the right version of it to be called. Everywhere else in the program
    the correct version is called. Here's the function declarations:

    template <class NumT, class ExpT>
    NumT rppower (NumT const x, ExpT const y);
    template <class NumT, class ExpT>
    SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);
    template <class NumT, class ExpT>
    SafeInt<NumT> rppower (SafeInt<NumT> const x, ExpT const y);
    template <class NumT, class ExpT>
    SafeInt<NumT> rppower (NumT const x, SafeInt<ExpT> const y);

    And here's the offending call and containing function. The function is
    abbreviated, but the relevant parts should be there:

    template <class T> T ArbInt::Value () const {
    SafeInt<T> retval;
    // _DigsT is vector<unsigned int>
    for (typename _DigsT::iterator i = _digits->begin();
    i != _digits->end(); ++i) {
    retval += *i * rppower(SafeInt<T>(s_digitbase),
    (i - _digits->begin()));
    }
    return retval;
    |

    For some reason, the first version of rppower is always called, with
    NumT being a SafeInt<T>. SafeInt has a casting operator to T's type, but
    that shouldn't be being called if there's a better match, and in other
    areas it's called correctly. I've tried explicitly specifying rppower<T,
    _DigsT::size_type>, I've tried making both arguments to rppower a
    SafeInt, and I've even tried making the first version of rppower the
    last one declared. Oh, and the * operator is overloaded for unsigned int
    * a SafeInt.

    I know it's a crazy mess of overloaded functions and operators, but
    maybe something will stick out. Here's hoping.

    Thanks!
    - Dave
    Dave Corby, Oct 12, 2005
    #1
    1. Advertising

  2. Dave Corby wrote:
    > Hi all,
    >
    > I have an overloaded template function, and in one particular spot can't
    > get the right version of it to be called. Everywhere else in the program
    > the correct version is called. Here's the function declarations:


    []

    > I know it's a crazy mess of overloaded functions and operators, but
    > maybe something will stick out. Here's hoping.


    I made your code compilable and it calls the proper overload as one
    would expect.

    #include <cstddef>

    template<class T> struct SafeInt
    {
    SafeInt(T);
    };

    template <class NumT, class ExpT>
    NumT rppower (NumT const x, ExpT const y);

    template <class NumT, class ExpT>
    SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);

    template <class NumT, class ExpT>
    SafeInt<NumT> rppower (SafeInt<NumT> const x, ExpT const y);

    template <class NumT, class ExpT>
    SafeInt<NumT> rppower (NumT const x, SafeInt<ExpT> const y);

    int main()
    {
    SafeInt<unsigned> r = rppower(SafeInt<unsigned>(1),
    (ptrdiff_t(1)));
    }

    g++ -Wall -c exp.cpp -o exp.o
    g++ -o exp exp.o
    exp.o(.text+0x26): In function `main':
    /home/max/src/exp/exp.cpp:24: undefined reference to `SafeInt<unsigned
    int>::SafeInt(unsigned int)'
    exp.o(.text+0x3f):/home/max/src/exp/exp.cpp:24: undefined reference to
    `SafeInt<unsigned int> rppower<unsigned int, int>(SafeInt<unsigned
    int>, int)'

    Note the second undefined reference - it tells you which overload has
    been selected.
    Maxim Yegorushkin, Oct 12, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    3
    Views:
    492
    Victor Bazarov
    Feb 13, 2005
  2. Ruki
    Replies:
    1
    Views:
    698
  3. aravindap
    Replies:
    2
    Views:
    369
    aravindap
    Oct 24, 2008
  4. somenath
    Replies:
    1
    Views:
    108
    bblaz
    Jan 7, 2014
  5. Replies:
    1
    Views:
    106
    Ken-Yi Lee
    Feb 2, 2014
Loading...

Share This Page