473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

template-based test function for unary operators?

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::operator+=;
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)"<templatedi.e. to "A&(*func)(const A&)"?

Sure some beginner mistake somewhere... :)

Any ideas?

- Jan
Oct 3 '08 #1
3 1569
JanW wrote:
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::operator+=;
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)"<templatedi.e. to "A&(*func)(const A&)"?

Sure some beginner mistake somewhere... :)

Any ideas?
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".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '08 #2
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::operator== 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
Oct 3 '08 #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::operator+=;
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.
Oct 3 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc Schellens | last post by:
I posted a similar question some time ago, but didn't get an satisfying answer. Lets say I have a template and five integer and two floating types. template <typename T> class A { A() {}...
14
by: LRS Kumar | last post by:
The following code - from "C++ Templates: The Complete Guide" by Vandevoorde/Josuttis - seems to compile with Borland C++. However it fails to compile with other compilers - Comeau Online and g++....
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
2
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
1
by: matz2k | last post by:
I've got a big problem with the CSS layout which I've produced with Photoshop/Dreamweaver especially for my ebay auctions. This is what it looks like...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.