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