CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    25

    [RESOLVED] can operator() be a member function template

    Hello all,

    I want to overload operator() of a class with a template function like below
    //---------------------------
    class A {
    public:

    template< int type >
    void
    operator()() {
    int i;
    }
    };

    int main() {
    A an_a;
    an_a()<100>;
    }
    //--------------------

    but i get the following errors (gcc 4.3.3):
    file.cpp: In function ‘int main()’:
    file.cpp:14: error: no match for call to ‘(A) ()’
    file.cpp:14: error: expected primary-expression before ‘;’ token

    what is the problem?

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: can operator() be a member function template

    You have to use the full operator name to invoke the method. It fails because the < operator has a higher precendence than the template argument bracket and the compiler tries to evaluate (anA < 100) > (), which is no valid syntax.

    Code:
    // works:
    anA.operator()<100>();
    
    // fails:
    anA<100>();
    Last edited by GNiewerth; January 8th, 2010 at 03:33 AM.
    - Guido

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: [RESOLVED] can operator() be a member function template

    Nice to learn that.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured