473,549 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting operators - why doesn't this code work ?

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_c lass( )
{
return my_wrapper<my_c lass>(*this);
}
};

int main( int argc, char **argv )
{
my_class instance;

my_function(ins tance);

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_functio n" call with the "my_class" argument. If I provide the
following function implementation :

void function( my_wrapper<my_c lassconst &arg )
{
std::cout << "in specialized function\n";
}

Everything works fine as an implicit call to the "my_class::oper ator
my_wrapper<my_c lass>( )" will be performed to match the necessary
argument type of "my_functio n". But on the other hand, If I provide
the following implementation for "my_functio n" :

template< typename _Tag >
void my_function( wrapper<_Tagcon st &arg )
{
std::cout << "in my_function\n";
}

The compiler will complain again that there is no match for the call
to "my_functio n" 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_functio n", 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_functio n" function
or by explicitly creating a "my_wrapper " object ?

Thanks for your help and comments :)

Olivier.

May 26 '07 #1
4 1421

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_c lass( ){
return my_wrapper<my_c lass>(*this);
}
};

int main( int argc, char **argv ){
my_class instance;
my_function(ins tance);
return 0;
}
[snip]
template< typename _Tag >
void my_function( wrapper<_Tagcon st &arg ){
std::cout << "in my_function\n";
}
Did you define 'wrapper<>' (or is that a typo)?

--
Bob R
POVrookie
May 26 '07 #2
On 26 May, 19:27, "BobR" <removeBadB...@ worldnet.att.ne twrote:
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_c lass( ){
return my_wrapper<my_c lass>(*this);
}
};
int main( int argc, char **argv ){
my_class instance;
my_function(ins tance);
return 0;
}

[snip]
template< typename _Tag >
void my_function( wrapper<_Tagcon st &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_functio n" should read :

template< typename _Tag >
void my_function( my_wrapper<_Tag const &arg )
{
std::cout << "in my_function\n";
}

Thanks,

Olivier.

May 26 '07 #3

casul <ol***********@ gmail.comwrote in message ...
On 26 May, 19:27, "BobR" <removeBadB...@ worldnet.att.ne twrote:
[snip]
template< typename _Tag >
void my_function( wrapper<_Tagcon st &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_functio n" 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_c lass>( 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
On 26 May, 23:59, "BobR" <removeBadB...@ worldnet.att.ne twrote:
casul <olivier.gr...@ gmail.comwrote in message ...
On 26 May, 19:27, "BobR" <removeBadB...@ worldnet.att.ne twrote:
[snip]
template< typename _Tag >
void my_function( wrapper<_Tagcon st &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_functio n" 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_c lass>( 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_c lassobject 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1902
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
16
2423
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new CImplementation(); pInterface->Method(); Case 2:
3
2327
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T> refcount { ... // reference counting wrapper; like shared_ptr }; class foo {
10
1619
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In particular I want to implement the op_Assign (=) operator for a __value struct.
6
6384
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. Of course it first tries to obtain the appropriate TypeConverter. However, for some types there are no applicable type converters. Consider a...
18
2156
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
14
2213
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit conversion" for a cast operation
2
1601
by: Giulio Petrucci | last post by:
Hi everybody, here's my problem: I have to dymanically build (and compile, of course) some code, from some ECMAScript function. ECMAScript variables I get are not typezed, so I should have operators like variable == a string or a double ore anything else in C# I thought to build a classes that wraps an object (untypized) and typize it...
4
15327
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it necessary to explicitly cast from one type of unsigned integer type to another even though they do so implicitly?
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.