| View previous topic :: View next topic |
| Author |
Message |
spamadress@bigfoot.com Guest
|
Posted: Wed Sep 28, 2005 3:59 pm Post subject: Why can type conversion operator not be qualified with expli |
|
|
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
|
Posted: Wed Sep 28, 2005 4:09 pm Post subject: Re: Why can type conversion operator not be qualified with e |
|
|
[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
|
Posted: Wed Sep 28, 2005 4:18 pm Post subject: Re: Why can type conversion operator not be qualified with e |
|
|
[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
|
Posted: Wed Sep 28, 2005 4:38 pm Post subject: Re: Why can type conversion operator not be qualified with e |
|
|
[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 |
|
 |
|