473,386 Members | 1,820 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,386 software developers and data experts.

casting operators - why doesn't this code work ?

Hi All,

Given the following code that just defines a dummy class "my_class"
and a dummy wrapper "my_wrapper" with a main program :

#include <iostream>

template< typename _Tag >
class my_wrapper
{
public:
my_wrapper( _Tag const &tag )
: _M_tag(tag)
{
}

private:
_Tag const &_M_tag;
};

class my_class
{
public:
operator my_wrapper<my_class( )
{
return my_wrapper<my_class>(*this);
}
};

int main( int argc, char **argv )
{
my_class instance;

my_function(instance);

return 0;
}

Now during compilation, and only given the above code, of course, the
compiler is going to complain that there is no match for the
"my_function" call with the "my_class" argument. If I provide the
following function implementation :

void function( my_wrapper<my_classconst &arg )
{
std::cout << "in specialized function\n";
}

Everything works fine as an implicit call to the "my_class::operator
my_wrapper<my_class>( )" will be performed to match the necessary
argument type of "my_function". But on the other hand, If I provide
the following implementation for "my_function" :

template< typename _Tag >
void my_function( wrapper<_Tagconst &arg )
{
std::cout << "in my_function\n";
}

The compiler will complain again that there is no match for the call
to "my_function" with the "my_class" instance as the argument. Why is
that ? I would have though the compiler could have resolved the call
using the similar mechanism it used to resolve the call with the
previous definition of "my_function", but apparently not.

Is this limitation due to the fact that compilers don't do that kind
of lookup, or is it a language limitation directly ? And if it is a
language limitation, why does such a limitation exist ?

If function call resolution were to take into consideration casting
operators and template arguments, that would allow for a generic
implementation of my function with any kind of user defined type given
that the type declares a simple casting operator which would be
usefull for serveral things.

Is there any other way to obtain the same behaviour for the code
without defining all possible variations of the "my_function" function
or by explicitly creating a "my_wrapper" object ?

Thanks for your help and comments :)

Olivier.

May 26 '07 #1
4 1414

casul <ol***********@gmail.comwrote in message ...
Hi All,
Given the following code that just defines a dummy class "my_class"
and a dummy wrapper "my_wrapper" with a main program :

#include <iostream>

template< typename _Tag class my_wrapper{
public:
my_wrapper( _Tag const &tag )
: _M_tag(tag){}
private:
_Tag const &_M_tag;
};

class my_class{
public:
operator my_wrapper<my_class( ){
return my_wrapper<my_class>(*this);
}
};

int main( int argc, char **argv ){
my_class instance;
my_function(instance);
return 0;
}
[snip]
template< typename _Tag >
void my_function( wrapper<_Tagconst &arg ){
std::cout << "in my_function\n";
}
Did you define 'wrapper<>' (or is that a typo)?

--
Bob R
POVrookie
May 26 '07 #2
On 26 May, 19:27, "BobR" <removeBadB...@worldnet.att.netwrote:
casul <olivier.gr...@gmail.comwrote in message ...
Hi All,
Given the following code that just defines a dummy class "my_class"
and a dummy wrapper "my_wrapper" with a main program :
#include <iostream>
template< typename _Tag class my_wrapper{
public:
my_wrapper( _Tag const &tag )
: _M_tag(tag){}
private:
_Tag const &_M_tag;
};
class my_class{
public:
operator my_wrapper<my_class( ){
return my_wrapper<my_class>(*this);
}
};
int main( int argc, char **argv ){
my_class instance;
my_function(instance);
return 0;
}

[snip]
template< typename _Tag >
void my_function( wrapper<_Tagconst &arg ){
std::cout << "in my_function\n";
}

Did you define 'wrapper<>' (or is that a typo)?

--
Bob R
POVrookie
That's a typo, sorry :) the code for "my_function" should read :

template< typename _Tag >
void my_function( my_wrapper<_Tagconst &arg )
{
std::cout << "in my_function\n";
}

Thanks,

Olivier.

May 26 '07 #3

casul <ol***********@gmail.comwrote in message ...
On 26 May, 19:27, "BobR" <removeBadB...@worldnet.att.netwrote:
[snip]
template< typename _Tag >
void my_function( wrapper<_Tagconst &arg ){
std::cout << "in my_function\n";
}
Did you define 'wrapper<>' (or is that a typo)?
That's a typo, sorry :) the code for "my_function" should read :

template< typename _Tag >
void my_function( my_wrapper<_Tagconst &arg ){
std::cout << "in my_function\n";
}

Thanks, Olivier.
Try this:

int main(){
my_class instance;

my_function( my_wrapper<my_class>( instance ) );

return 0;
}

Also, please do not quote signatures ( parts after "--" ). Some newsreaders
strip anything after that line, so, some people never see your reply.
--
Bob R
POVrookie
May 26 '07 #4
On 26 May, 23:59, "BobR" <removeBadB...@worldnet.att.netwrote:
casul <olivier.gr...@gmail.comwrote in message ...
On 26 May, 19:27, "BobR" <removeBadB...@worldnet.att.netwrote:
[snip]
template< typename _Tag >
void my_function( wrapper<_Tagconst &arg ){
std::cout << "in my_function\n";
}
Did you define 'wrapper<>' (or is that a typo)?
That's a typo, sorry :) the code for "my_function" should read :
template< typename _Tag >
void my_function( my_wrapper<_Tagconst &arg ){
std::cout << "in my_function\n";
}
Thanks, Olivier.

Try this:

int main(){
my_class instance;

my_function( my_wrapper<my_class>( instance ) );

return 0;
}

Also, please do not quote signatures ( parts after "--" ). Some newsreaders
strip anything after that line, so, some people never see your reply.
Hi,

Thanks for the reply, and comment noted regarding quoting
signatures :)

Yes that works, but i want to be able to do without the explicit call
to the my_wrapper<my_classobject constructor. And if that is not
possible, i'd like to understand why there is such a restriction on
function call resolution.

O.
May 27 '07 #5

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

Similar topics

13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
3
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T>...
10
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In...
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
2
by: Giulio Petrucci | last post by:
Hi everybody, here's my problem: I have to dymanically build (and compile, of course) some code, from some ECMAScript function. ECMAScript variables I get are not typezed, so I should have...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.