473,608 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type conversion

frs
See example below: Why does the output of 'a' work and the output of
'b' fails to compile? Is there a way to write class 'something' so that
'b' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<strin g> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank

Sep 3 '05 #1
16 2976
frs wrote:
See example below: Why does the output of 'a' work and the output of
'b' fails to compile? Is there a way to write class 'something' so that
'b' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<strin g> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank


It's probably got something to do with the fact that string itself is a
template.

For instance the following does compile

template <typename T>
struct something {
T x;
operator T();
};

struct S
{
};

ostream& operator<<(ostr eam&, const S&);

int main(int, char**)
{
something<int> a;
something<S> b;

a.x = 4711;
b.x = S();

cout << a << endl; // compiles fine!
cout << b << endl; // compiles fine!
}

I don't know the complete answer to your question but really my advice
would be to stay away from this stuff. I think even BS said stay away
from the obscure corners of the language.

John
Sep 3 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:TJ******** ******@newsfe7-win.ntli.net...
frs wrote:
See example below: Why does the output of 'a' work and the output of
'b' fails to compile? Is there a way to write class 'something' so that
'b' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<strin g> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank


It's probably got something to do with the fact that string itself is a
template.

For instance the following does compile

template <typename T>
struct something {
T x;
operator T();
};

struct S
{
};

ostream& operator<<(ostr eam&, const S&);

int main(int, char**)
{
something<int> a;
something<S> b;

a.x = 4711;
b.x = S();

cout << a << endl; // compiles fine!
cout << b << endl; // compiles fine!
}

I don't know the complete answer to your question but really my advice
would be to stay away from this stuff. I think even BS said stay away from
the obscure corners of the language.

John


Had that been the case then the line

something<strin g> b;

would have failed to compile.

One possibility is that the string used wasn't std::string and doesn't
provide output operation with streams.

Ben
Sep 3 '05 #3
hmm, it really didn't compile with std::string...I can't figure out what's
the cause...

Anyway the following will do what the OP might want to do:

template <typename T>
struct something
{
T x;
};

template <typename OStreamT, typename T>
OStreamT& operator << (
OStreamT& os, const something<T>& o)
{
return os << o.x;
}

int main(void)
{
something<int> a;
something<std:: string> b;

a.x = 4711;
b.x = "hello world!";

std::cout << a << std::endl;
std::cout << b << std::endl;
}

ben
Sep 3 '05 #4
benben wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:TJ******** ******@newsfe7-win.ntli.net...
frs wrote:
See example below: Why does the output of 'a' work and the output of
'b' fails to compile? Is there a way to write class 'something' so that
'b' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<strin g> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank


It's probably got something to do with the fact that string itself is a
template.

For instance the following does compile

template <typename T>
struct something {
T x;
operator T();
};

struct S
{
};

ostream& operator<<(ostr eam&, const S&);

int main(int, char**)
{
something<int> a;
something<S> b;

a.x = 4711;
b.x = S();

cout << a << endl; // compiles fine!
cout << b << endl; // compiles fine!
}

I don't know the complete answer to your question but really my advice
would be to stay away from this stuff. I think even BS said stay away from
the obscure corners of the language.

John

Had that been the case then the line

something<strin g> b;

would have failed to compile.


Huh? That's a completely different situation.

John
Sep 3 '05 #5
benben wrote:
hmm, it really didn't compile with std::string...I can't figure out what's
the cause...


I don't know the reason either, what I do know is that the rules
governing this kind of thing are extremely complex (try reading the C++
standard) and often have non-intuitive effects. That is why the advice
is to stay away from obscure corners of the language.

John
Sep 3 '05 #6
> I don't know the reason either, what I do know is that the rules governing
this kind of thing are extremely complex (try reading the C++ standard)
and often have non-intuitive effects. That is why the advice is to stay
away from obscure corners of the language.

John


You are probably right on that the problem may originate from that fact that
string in itself is an instantiation of a class template because of the need
to instantiate a copy of the overloaded operator << function. I played
around with the code...like the one below:

#include <iostream>
#include <string>

template <typename T>
struct something
{
T x;
operator T(){return x;}
};

template <typename T>
class some_curious_st uff{};

template <typename T, typename OS>
OS& operator << (OS& os, const some_curious_st uff<T>&)
{
return os << "curious stuff";
}

// explicitly instantiate a copy of operator <<
template std::ostream& operator <<(std::ostream &, const
some_curious_st uff<int>&);

/*
Commented out operator overloading below:
if I uncomment the code below and comment out the explicit
instantiation the code gets compiled...
*/
//
//std::ostream& operator << (std::ostream& os, const
some_curious_st uff<int>&)
//{
// return os << "curious int stuff";
//}

int main(void)
{
some_curious_st uff<int> a;
std::cout << a;

something<some_ curious_stuff<i nt> > b;
std::cout << b; // Still ERROR
}
My impression is the explicitly instantiated operator << is not
overloaded....s omehow...

Ben
Sep 3 '05 #7

frs wrote:
See example below: Why does the output of 'a' work and the output of
'b' fails to compile? Is there a way to write class 'something' so that
'b' converts correctly by default? (include iostream, string, use
namespace std)
template <typename T>
struct something {
T x;
operator T();
};

int
main(int, char**)
{
something<int> a;
something<strin g> b;

a.x = 4711;
b.x = "hello world!";

cout << a << endl; // compiles fine!
cout << b << endl; // operator<< ... no match, compile fails!
}
compiler = gcc 3.3.5

Thanks for any Help.

Frank


user-defined conversion operators are not considered when the compiler
tries to deduce template arguments for the operator<< call. you could:

(1) provide operator<< and operator>> for your something class.

(2) derive from T instead of having a T member:

template<typena me T>
struct something : T {};

then provide template specializations for primitive types, that perhaps
use composition like your original solution. This is definitely the
more questionable solution.

Sep 3 '05 #8
I think the problem is due to the fact that your are using templates
and overloading at the same time when you insert b into cout.

When you insert 'a' into cout you are calling a member function of cout
(although it is a template all the instantation can happen based on the
class which is obviously basic_ostream<c har>).

template <typename T>
basic_ostream<T >&
baisc_ostream<T >::operator<<(i nt value);

All the compiler has to do here is realize that an implict cast to int
is possible and that one of the member functions of baisc_ostream takes
int.

When you try to insert 'b' you are calling a template function:

template < typename charT >
basic_ostream< charT >&
operator << ( basic_ostream < charT >& is,
basic_string< charT > )

In order for this function to be called two things would need to happen
1. the compiler would need to realize that an implicit conversion to
basic_string<ch ar> is possible.
2. The template would need to be instantated for basic_string<ch ar>

This doesn't happen because template resolution happens before the
compiler starts looking for implicit casts. This means that the
function for inserting basic_string<T> is never considered and you get
an error message saying an appropiate operator << cannot be found.

I hope this makes sense (and is correct).

Sep 3 '05 #9
frs
Thanks,

I think you hit it straight to the point.

Frank.

Sep 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1676
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s) { this.s=s;
7
2236
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the collection. Thus, my interface now looks like this public interface IMyCollection : ICollection { void Add (string toBeAdded);
27
5625
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
1875
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict with me... using System.Collections.Generic;
16
12780
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
1877
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version: $Revision: 42333 $
1
3271
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal? Is what Keith Thompson said in another post also helpful to understand this question: "... The implicit conversion rule applies only to void*, not...
669
25801
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
4
2099
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b),
8
3141
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the developer. x = someObject as MySpecificType; x = (MySpecificType) someObject; Thanks.
0
8488
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8164
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
6831
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5489
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
3977
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4039
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2482
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
1
1613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1345
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.