C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why can type conversion operator not be qualified with expli

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
spamadress@bigfoot.com
Guest





PostPosted: Wed Sep 28, 2005 3:59 pm    Post subject: Why can type conversion operator not be qualified with expli Reply with quote



Hello

I wonder why only constructors can be qualified with explicit to
prevent implicit conversion, but not conversion operators. To me the
following makes perfectly sense:

struct A {
explicit operator int() { return 0;} ;
};
void foo(int) {};
int main() {
A a;
foo( a ); // implicit conversion: should be prohibited by explicit
foo( static_cast<int>(a) ); // explicit conversion: ok
return 0;
}

Greetings

Flo

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Sep 28, 2005 4:09 pm    Post subject: Re: Why can type conversion operator not be qualified with e Reply with quote



[email]spamadress (AT) bigfoot (DOT) com[/email] wrote:
Quote:

Hello

I wonder why only constructors can be qualified with explicit to
prevent implicit conversion, but not conversion operators. To me the
following makes perfectly sense:

struct A {
explicit operator int() { return 0;} ;
};
void foo(int) {};
int main() {
A a;
foo( a ); // implicit conversion: should be prohibited by explicit
foo( static_cast<int>(a) ); // explicit conversion: ok
return 0;
}

Greetings


I can only guess:
Because you could write an ordinary function to do what you want to do?

struct A {
int AsInt() { return 0; }
};

void foo(int) {};
int main()
{ A a;
foo( a ); // implicit conversion: should be prohibited by explicit
foo( a.AsInt() ); // explicit conversion: ok
return 0;
}

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Victor Bazarov
Guest





PostPosted: Wed Sep 28, 2005 4:18 pm    Post subject: Re: Why can type conversion operator not be qualified with e Reply with quote



[email]spamadress (AT) bigfoot (DOT) com[/email] wrote:
Quote:
I wonder why only constructors can be qualified with explicit to
prevent implicit conversion, but not conversion operators. To me the
following makes perfectly sense:

struct A {
explicit operator int() { return 0;} ;
};
void foo(int) {};
int main() {
A a;
foo( a ); // implicit conversion: should be prohibited by explicit
foo( static_cast<int>(a) ); // explicit conversion: ok
return 0;
}

IIRC, there is a defect report on that... Lemme see... Nope, can't find
any... But I remember conversations about it. Probably in the newsgroup
archives... Let's go there... Yep. Plenty of discussions. Try this:

http://groups.google.com/groups?q=type+conversion+operator+explicit&hl=en

V

Back to top
Julián Albo
Guest





PostPosted: Wed Sep 28, 2005 4:38 pm    Post subject: Re: Why can type conversion operator not be qualified with e Reply with quote

[email]spamadress (AT) bigfoot (DOT) com[/email] wrote:

Quote:
I wonder why only constructors can be qualified with explicit to
prevent implicit conversion, but not conversion operators. To me the
following makes perfectly sense:

struct A {
explicit operator int() { return 0;} ;
};
void foo(int) {};
int main() {
A a;
foo( a ); // implicit conversion: should be prohibited by explicit
foo( static_cast<int>(a) ); // explicit conversion: ok
return 0;
}

Probaby because is easy and more legible to write a: 'int to_int ();' member
function.

--
Salu2

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.