Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting operators - why doesn't this code work ?

Reply
Thread Tools

casting operators - why doesn't this code work ?

 
 
casul
Guest
Posts: n/a
 
      05-26-2007
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: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.

 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      05-26-2007

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


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

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
 
casul
Guest
Posts: n/a
 
      05-26-2007
On 26 May, 19:27, "BobR" <(E-Mail Removed)> wrote:
> casul <(E-Mail Removed)> wrote 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<_Tag> const &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<_Tag> const &arg )
{
std::cout << "in my_function\n";
}

Thanks,

Olivier.

 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      05-26-2007

casul <(E-Mail Removed)> wrote in message ...
> On 26 May, 19:27, "BobR" <(E-Mail Removed)> wrote:

[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.


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


 
Reply With Quote
 
casul
Guest
Posts: n/a
 
      05-26-2007
On 26 May, 23:59, "BobR" <(E-Mail Removed)> wrote:
> casul <(E-Mail Removed)> wrote in message ...
> > On 26 May, 19:27, "BobR" <(E-Mail Removed)> wrote:

> [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.

>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templated Casting operators Narinder C++ 0 07-07-2011 11:32 PM
casting and ++ operators !! duggydiggy C Programming 2 09-30-2009 03:02 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
having trouble with casting operators.. JustSomeGuy C++ 7 08-22-2005 12:45 AM
need help with casting operators... JustSomeGuy C++ 13 06-11-2004 09:24 AM



Advertisments