By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

casting operators - why doesn't this code work ?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.