473,594 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can type conversion operator not be qualified with explicit?

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

Sep 28 '05 #1
3 2099
sp********@bigf oot.com wrote:

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
kb******@gascad .at
Sep 28 '05 #2
sp********@bigf oot.com wrote:
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=ty...explicit&hl=en

V
Sep 28 '05 #3
sp********@bigf oot.com wrote:
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
Sep 28 '05 #4

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

Similar topics

4
2413
by: Jason Heyes | last post by:
I want Foo-type objects to be convertible to Bar-type objects. Can I use operator Bar() to do this just as I would use operator bool() to convert to boolean? Example: class Foo { public: operator Bar() const; };
4
3209
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
9
4027
by: Roman Mashak | last post by:
Hello, All! Given the sample piece of code I have: #include <stdio.h> #include <string.h> int main(void) { short int i, j;
4
1675
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;
4
1435
by: Girish | last post by:
Ok, starting from the basics. Reference types and value types are all objects underneath. So, a String is an object as well. Now, when when we are debugging and we examine the contents of a String object - the debugger actually shows the value of containing string object when you keep a watch on the instance variable. But when I create my OWN type which holds a string inside - and get this
27
5624
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 {
6
2425
by: Joseph Turian | last post by:
I want to create several types of double, e.g. D1 and D2. I want strong typing, such that a function defined for D1 will not accept a D2 argument. One solution is: struct D1 { double d; }; However, the usage is cumbersome (I have to access member variable d),
3
4608
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string> #include <iostream> #include <cstring> int main(){
8
3139
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
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
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
8251
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...
0
8372
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...
0
8234
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...
0
6654
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
5408
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
3859
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
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.