casting operators - why doesn't this code work ?

C

casul

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_class> const &arg )
{
std::cout << "in specialized function\n";
}

Everything works fine as an implicit call to the "my_class::eek:perator
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<_Tag> const &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.
 
B

BobR

casul said:
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<_Tag> const &arg ){
std::cout << "in my_function\n";
}

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

casul

casul said:
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<_Tag> const &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<_Tag> const &arg )
{
std::cout << "in my_function\n";
}

Thanks,

Olivier.
 
B

BobR

casul said:
That's a typo, sorry :) the code for "my_function" should read :

template< typename _Tag >
void my_function( my_wrapper<_Tag> const &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.
 
C

casul

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_class> object constructor. And if that is not
possible, i'd like to understand why there is such a restriction on
function call resolution.

O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top