template-based test function for unary operators?

Discussion in 'C++' started by JanW, Oct 3, 2008.

  1. JanW

    JanW Guest

    Somewhat a C++ beginner, I'm trying to make a general test function
    that could test unary operators (or methods) of an object of any class.

    Arguments are a member-pointer to the function, a single input
    argument of some type for that function, and the expected result of
    some type. Then it tests if the result is as expected, and does some
    logging of the results (pass, fail) etc.

    Well, the template "mess" does not quite work out.

    Below is the code. After that, the error messages.

    --------------------------------------------------------

    class A {
    int f (int c) { return c; }
    int f (const char c) { return c; }
    A& operator+= (A const& a) { return *this; }
    };

    template <class rettype, class argtype, class btype>
    bool unary_op_test(rettype (*func)(argtype),
    btype self, argtype arg, rettype expect)
    {
    rettype rv = self.func(arg);
    bool result = (rv == expect);
    return result;
    }

    int main ()
    {
    A aa;
    int (A::*p1)(int) = &A::f;
    A& (A::*p2)(A const&) = &A::eek:perator+=;
    unary_op_test<int,int,A&>(p1, aa, 12, 12);
    unary_op_test<A&,A&,A&>(p2, aa, aa, aa);
    unary_op_test<A&,const A&,A&>(p2, aa, aa, aa);
    }

    --------------------------------------------------------

    $ g++ -Wall -pedantic test.cc
    test.cc: In function 'int main()':
    test.cc:21: error: no matching function for call to
    'unary_op_test(int (A::*&)(int), A&, int, int)'
    test.cc:22: error: no matching function for call to
    'unary_op_test(A& (A::*&)(const A&), A&, A&, A&)'
    test.cc:23: error: no matching function for call to
    'unary_op_test(A& (A::*&)(const A&), A&, A&, A&)'

    --------------------------------------------------------

    How come "A& (A::*&)(const A&)" does not map to "rettype
    (*func)(argtype)"<templated> i.e. to "A&(*func)(const A&)"?

    Sure some beginner mistake somewhere... :)

    Any ideas?

    - Jan
     
    JanW, Oct 3, 2008
    #1
    1. Advertising

  2. JanW

    JanW Guest

    Victor Bazarov wrote:
    > JanW wrote:

    <snip>
    > Only one idea so far: the type "a pointer to a [non-static] member
    > function of class T" is not convertible to "a pointer to function".


    Oops, true. That was quick, thanks :)

    After the first attempts it has now started working. As a first
    version, just rewriting unary_op_test() to like below (and defining
    A::eek:perator== in class A) removes the complaints:

    template <class rettype, class argtype, class btype>
    bool unary_op_test(rettype (btype::* func)(argtype), btype& self,
    argtype arg, rettype expect)
    {
    rettype rv = (self.*func)(arg);
    bool result = (rv == expect);
    return result;
    }

    And then call the tests like below:

    unary_op_test<int,int,A>(p1, aa, 12, 12);
    unary_op_test<A&,const A&,A>(p2, aa, aa, aa);
    unary_op_test(p1, aa, 12, 12);

    Thanks!

    - Jan
     
    JanW, Oct 3, 2008
    #2
    1. Advertising

  3. JanW wrote:
    > Well, the template "mess" does not quite work out.
    >
    > Below is the code. After that, the error messages.
    >
    > --------------------------------------------------------
    >
    > class A {
    > int f (int c) { return c; }
    > int f (const char c) { return c; }
    > A& operator+= (A const& a) { return *this; }
    > };
    >
    > template <class rettype, class argtype, class btype>
    > bool unary_op_test(rettype (*func)(argtype),
    > btype self, argtype arg, rettype expect)
    > {
    > rettype rv = self.func(arg);
    > bool result = (rv == expect);
    > return result;
    > }
    >
    > int main ()
    > {
    > A aa;
    > int (A::*p1)(int) = &A::f;
    > A& (A::*p2)(A const&) = &A::eek:perator+=;
    > unary_op_test<int,int,A&>(p1, aa, 12, 12);
    > unary_op_test<A&,A&,A&>(p2, aa, aa, aa);
    > unary_op_test<A&,const A&,A&>(p2, aa, aa, aa);
    > }


    Pointers to member functions are not pointers to functions, and there's
    no implicit conversion. There are other issues in your code, but this is
    the main one. Here's a rough function template accepting a pointer to
    member function:

    template< typename Ret, typename C, typename Arg>
    void f( Ret ( C::*pmf )( Arg ),
    C & c,
    Arg a )
    {
    ( c.*pmf )( a ) ;
    }

    usable as follows:

    A instance = {} ;
    int ( A::*p1 )( int ) = &A::f;
    f( p1, instance, 12 ) ;

    --
    Gennaro Prota | name.surname yahoo.com
    Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
    Do you need expertise in C++? I'm available.
     
    Gennaro Prota, Oct 3, 2008
    #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.
Similar Threads
  1. Ruben Campos
    Replies:
    5
    Views:
    558
    Ruben Campos
    Nov 30, 2004
  2. Ashwin  N

    Associativity of unary operators

    Ashwin N, Sep 8, 2006, in forum: Java
    Replies:
    4
    Views:
    1,732
    Stefan Ram
    Sep 9, 2006
  3. dspfun

    Associativity of unary C Operators

    dspfun, Jan 1, 2007, in forum: C Programming
    Replies:
    28
    Views:
    887
    CBFalconer
    Jan 5, 2007
  4. SpOiLeR
    Replies:
    10
    Views:
    811
    SpOiLeR
    Oct 19, 2005
  5. mathog

    historical question, C unary operators

    mathog, Mar 29, 2012, in forum: C Programming
    Replies:
    13
    Views:
    1,399
    Eric Sosman
    Mar 30, 2012
Loading...

Share This Page