function template and operator () overload

Discussion in 'C++' started by Fei Liu, Feb 24, 2006.

  1. Fei Liu

    Fei Liu Guest

    Hi, I have a interesting problem here,

    class absOP{
    template<class T> T operator(T val) { return val < 0 ? -val : val; }
    };

    Now the problem is I can't seem to use this overloaded operator,

    absOP op(..);

    int i = -42;
    float f = -2.3;

    int ai = op(i); //error
    ai = op<int>(i); //error
    ai = op(i)<int>; //error
    ai = <int>op(i); //error

    Alas, I tried a couple sane ways to use function template of overloaded
    operator () but none seemed to work...Any idea or help is appreciated.
     
    Fei Liu, Feb 24, 2006
    #1
    1. Advertising

  2. Fei Liu wrote:
    > Hi, I have a interesting problem here,
    >
    > class absOP{
    > template<class T> T operator(T val) { return val < 0 ? -val : val; }


    Did you mean to write

    template<class T> T operator()(T val) { return val < 0 ? -val : val; }

    ? Notice the "empty set of parens" after the word 'operator'. It is also
    declared private at this point.

    > };
    >
    > Now the problem is I can't seem to use this overloaded operator,
    >
    > absOP op(..);
    >
    > int i = -42;
    > float f = -2.3;
    >
    > int ai = op(i); //error
    > ai = op<int>(i); //error
    > ai = op(i)<int>; //error
    > ai = <int>op(i); //error
    >
    > Alas, I tried a couple sane ways to use function template of overloaded
    > operator () but none seemed to work...Any idea or help is appreciated.


    This is covered by FAQ 5.8.

    #include <iostream>
    using namespace std;

    struct abs_op {
    template<class T> T operator()(T t) { return t > 0 ? t : -t; }
    };

    int main()
    {
    int i = -42, ii = 42;
    double d = 3.14159, dd = -3.14159;

    abs_op a;

    cout << a(i) << ' ' << a(ii) << ' ' << a(d) << ' ' << a(dd) << endl;
    }


    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Feb 24, 2006
    #2
    1. Advertising

  3. Fei Liu

    Fei Liu Guest

    Thanks, Victor. I am sorry I didn't put enough thought in this example.
    It's a little bit more complicated than the example I gave,
    class somedata{
    void * data;
    template<class T> T operator()(size_t index) { return
    *(reinterpret_cast<T*>data + index); }
    };

    somedata x(....); // data is initialized but actual type of data is
    unknown. data is an array
    float ele = x(3); // retrieve the 4th element of data array inside x,
    error
    float ele = <float>x(3); //error
    float ele = x<float>(3); //error
    float ele = x(3)<float>; //error

    I hope this made it clearer.

    Victor Bazarov wrote:
    > Fei Liu wrote:
    > > Hi, I have a interesting problem here,
    > >
    > > class absOP{
    > > template<class T> T operator(T val) { return val < 0 ? -val : val; }

    >
    > Did you mean to write
    >
    > template<class T> T operator()(T val) { return val < 0 ? -val : val; }
    >
    > ? Notice the "empty set of parens" after the word 'operator'. It is also
    > declared private at this point.
    >
    > > };
    > >
    > > Now the problem is I can't seem to use this overloaded operator,
    > >
    > > absOP op(..);
    > >
    > > int i = -42;
    > > float f = -2.3;
    > >
    > > int ai = op(i); //error
    > > ai = op<int>(i); //error
    > > ai = op(i)<int>; //error
    > > ai = <int>op(i); //error
    > >
    > > Alas, I tried a couple sane ways to use function template of overloaded
    > > operator () but none seemed to work...Any idea or help is appreciated.

    >
    > This is covered by FAQ 5.8.
    >
    > #include <iostream>
    > using namespace std;
    >
    > struct abs_op {
    > template<class T> T operator()(T t) { return t > 0 ? t : -t; }
    > };
    >
    > int main()
    > {
    > int i = -42, ii = 42;
    > double d = 3.14159, dd = -3.14159;
    >
    > abs_op a;
    >
    > cout << a(i) << ' ' << a(ii) << ' ' << a(d) << ' ' << a(dd) << endl;
    > }
    >
    >
    > V
    > --
    > Please remove capital As from my address when replying by mail
     
    Fei Liu, Feb 24, 2006
    #3
  4. Fei Liu wrote:
    > [...]


    First of all, please don't top-post. I've rearranged it.

    > Victor Bazarov wrote:
    >
    >>Fei Liu wrote:
    >>
    >>>Hi, I have a interesting problem here,
    >>>
    >>>class absOP{
    >>> template<class T> T operator(T val) { return val < 0 ? -val : val; }

    >>
    >>Did you mean to write
    >>
    >> template<class T> T operator()(T val) { return val < 0 ? -val : val; }
    >>
    >>? Notice the "empty set of parens" after the word 'operator'. It is also
    >>declared private at this point.
    >>
    >>
    >>>};
    >>>
    >>>Now the problem is I can't seem to use this overloaded operator,
    >>>
    >>>absOP op(..);
    >>>
    >>>int i = -42;
    >>>float f = -2.3;
    >>>
    >>>int ai = op(i); //error
    >>>ai = op<int>(i); //error
    >>>ai = op(i)<int>; //error
    >>>ai = <int>op(i); //error
    >>>
    >>>Alas, I tried a couple sane ways to use function template of overloaded
    >>>operator () but none seemed to work...Any idea or help is appreciated.

    >>
    >>This is covered by FAQ 5.8.
    >>
    >>#include <iostream>
    >>using namespace std;
    >>
    >>struct abs_op {
    >> template<class T> T operator()(T t) { return t > 0 ? t : -t; }
    >>};
    >>
    >>int main()
    >>{
    >> int i = -42, ii = 42;
    >> double d = 3.14159, dd = -3.14159;
    >>
    >> abs_op a;
    >>
    >> cout << a(i) << ' ' << a(ii) << ' ' << a(d) << ' ' << a(dd) << endl;
    >>}


    > Thanks, Victor. I am sorry I didn't put enough thought in this example.
    > It's a little bit more complicated than the example I gave,


    It's not just "more complicated". The template doesn't have the argument
    of type 'T', and that makes it _impossible_ to tell which 'T' is going to
    be used. The compiler cannot deduce it from 'size_t', and the fact that
    you're using the _operator_ syntax makes it impossible to provide any
    information for the deduction.

    > class somedata{
    > void * data;
    > template<class T> T operator()(size_t index) { return
    > *(reinterpret_cast<T*>data + index); }
    > };
    >
    > somedata x(....); // data is initialized but actual type of data is
    > unknown. data is an array
    > float ele = x(3); // retrieve the 4th element of data array inside x,
    > error
    > float ele = <float>x(3); //error
    > float ele = x<float>(3); //error
    > float ele = x(3)<float>; //error
    >
    > I hope this made it clearer.


    Yes. You're SOL. Don't use operator syntax. Convert to a regular
    function, like so

    class somedata {
    ...
    template<class T> T doit()(size_t index) ...
    };

    float ele = x.doit<float>(3);

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Feb 24, 2006
    #4
  5. Fei Liu

    Fei Liu Guest

    Unfortunately, I cannot convert it to a function. So it seems it's not
    possible to make it work when function template and operator () are
    used together the way I intended. Thanks again.
     
    Fei Liu, Feb 24, 2006
    #5
  6. Fei Liu wrote:
    > Unfortunately, I cannot convert it to a function. So it seems it's not
    > possible to make it work when function template and operator () are
    > used together the way I intended. Thanks again.


    It's possible.

    template<class T> struct type {};

    struct S
    {
    template<class T>
    T operator()(type<T>, size_t);
    };

    int main(int ac, char** av)
    {
    S s;
    s(type<int>(), 1);
    }
     
    Maxim Yegorushkin, Feb 25, 2006
    #6
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  2. Atlas
    Replies:
    13
    Views:
    500
    Atlas
    Oct 20, 2005
  3. fungus
    Replies:
    11
    Views:
    579
    fungus
    Jan 6, 2007
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
  5. 邹俊洋
    Replies:
    0
    Views:
    196
    邹俊洋
    Aug 12, 2013
Loading...

Share This Page