template<> operator()

Discussion in 'C++' started by Phil Endecott, Oct 3, 2007.

  1. Dear Experts,

    I find myself wanting to write

    class functor {
    template <typename T>
    T operator()(args) {...}
    };

    in order to

    functor f;
    sometype r = f<sometype>(3.14);


    but I can't get anything to work. Is operator() allowed to be a
    template function? (Maybe I am, because I only get errors at the point
    where I try to use it.) If it is allowed, how am I supposed to call it?

    Thanks for any clarification,

    Phil.
     
    Phil Endecott, Oct 3, 2007
    #1
    1. Advertising

  2. Phil Endecott

    Barry Guest

    Phil Endecott wrote:
    > Dear Experts,
    >
    > I find myself wanting to write
    >
    > class functor {
    > template <typename T>
    > T operator()(args) {...}
    > };
    >
    > in order to
    >
    > functor f;
    > sometype r = f<sometype>(3.14);


    sometype r = f.operator()<sometype>(3.14);

    when you don't have templated argument list for template operator to
    implicit instantiate. I suggest you write ordinary function rather than
    operator().

    >
    >
    > but I can't get anything to work. Is operator() allowed to be a
    > template function? (Maybe I am, because I only get errors at the point
    > where I try to use it.) If it is allowed, how am I supposed to call it?
    >
    > Thanks for any clarification,
    >
    > Phil.
     
    Barry, Oct 3, 2007
    #2
    1. Advertising

  3. Phil Endecott wrote:
    > Dear Experts,
    >
    > I find myself wanting to write
    >
    > class functor {
    > template <typename T>
    > T operator()(args) {...}
    > };
    >
    > in order to
    >
    > functor f;
    > sometype r = f<sometype>(3.14);
    >
    >
    > but I can't get anything to work. Is operator() allowed to be a
    > template function? (Maybe I am, because I only get errors at the
    > point where I try to use it.) If it is allowed, how am I supposed to
    > call it?


    It's allowed to be a template, but, like a constructor, the type has
    to be deducible from the arguments. For the function call operator
    the syntax is rather ugly

    sometype r = f.operator()<sometype>(3.14);

    so you probably don't want to use it. And there is no syntax at all
    for templated constructors.

    Now, since the syntax you want to use is unavailable, you're better off
    defining a named function instead of the operator(). Then you call it

    sometype r = f.funcname<sometype>(3.14);

    As to availability of the syntax, you might want to ask in comp.std.c++
    about the rationale behind disallowing it. My guess would be that the
    presence of the less-than operator after the name of an object is
    already reserved for comparison purposes. IOW

    sometype r = f < ...

    means you're comparing 'f' to the rest of the expression. It most
    likely would be difficult to allow you to have a template argument list
    after the object name in a rare case you've defined a function call
    operator as a template.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 3, 2007
    #3
    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.

Share This Page