473,587 Members | 2,443 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 1423

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
1911
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
2427
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
2328
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
1628
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
6391
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 custom class "Dummy" from a linked assembly (in my scenario, neither the Dummy type nor its assembly...
18
2162
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
14
2214
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
1602
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 inside it and to overload operators. In the code below there's an
4
15336
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
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5722
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.