G
Guest
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
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