casting operators - why doesn't this code work ?

Discussion in 'C++' started by casul, May 26, 2007.

  1. casul

    casul Guest

    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.
     
    casul, May 26, 2007
    #1
    1. Advertising

  2. casul

    BobR Guest

    casul <> 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
     
    BobR, May 26, 2007
    #2
    1. Advertising

  3. casul

    casul Guest

    On 26 May, 19:27, "BobR" <> wrote:
    > casul <> 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.
     
    casul, May 26, 2007
    #3
  4. casul

    BobR Guest

    casul <> wrote in message ...
    > On 26 May, 19:27, "BobR" <> 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
     
    BobR, May 26, 2007
    #4
  5. casul

    casul Guest

    On 26 May, 23:59, "BobR" <> wrote:
    > casul <> wrote in message ...
    > > On 26 May, 19:27, "BobR" <> 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.
     
    casul, May 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    need help with casting operators...

    JustSomeGuy, Jun 9, 2004, in forum: C++
    Replies:
    13
    Views:
    565
    dumboo
    Jun 11, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    906
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,978
    Smokey Grindel
    Dec 2, 2006
  4. JustSomeGuy
    Replies:
    7
    Views:
    318
    Victor Bazarov
    Aug 22, 2005
  5. stroncococcus

    type casting for DBI SQL statement doesn't work

    stroncococcus, Dec 6, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    168
    stroncococcus
    Dec 6, 2006
Loading...

Share This Page