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

Home Posts Topics Members FAQ

Different types to ?:


Why is it that if I have an overloaded function like:

void foo(int i);
void foo(char *c);

I can't do:

void bar() {
foo( condition ? 5 : "NULL");
}
As you might guess I'm building a query and my compiler (g++) complaines
about different operands to ?:

While I can easily replace the ?: with an if I was surprised that the
statement wasn't allowed. So I wondered if this really is specified in the
standard (or a quirk of g++) and there is any good reason for it to be
that way.

Anyone care to enlighten me?
regards
NPV
Jul 19 '05 #1
9 3998

"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message
news:pa******** *************** *****@spam.for. me.invalid...

Why is it that if I have an overloaded function like:

void foo(int i);
void foo(char *c);

I can't do:

void bar() {
foo( condition ? 5 : "NULL");
}
As you might guess I'm building a query and my compiler (g++) complaines
about different operands to ?:

While I can easily replace the ?: with an if I was surprised that the
statement wasn't allowed. So I wondered if this really is specified in the
standard (or a quirk of g++) and there is any good reason for it to be
that way.

Anyone care to enlighten me?
regards
NPV


Its not allowed. Any expression has a single type, which can be deduced at
compile time by the compiler. Your suggestion would mean that an expressions
type could not be determined until a program runs. Sounds like a nightmare
to me (and not only for compiler writers).

john
Jul 19 '05 #2
On Thu, 21 Aug 2003 11:19:41 +0100, John Harrison wrote:
Its not allowed. Any expression has a single type, which can be deduced at
compile time by the compiler. Your suggestion would mean that an expressions
type could not be determined until a program runs. Sounds like a nightmare
to me (and not only for compiler writers).


And how would that be harder than compiling

class Base;
class Child1;
class Child2;

foo(Base b);
foo(Child1 c);
foo(Child2 c);

bar(Base arg) {
foo(arg);
}

where you don't know the type of arg at compiletime?

Just because it may be hard for compiler writers is no reason that it
shouldn't be used, I expect the compiler writers have solved problems much
harder than that one.

Is there any reason that ?: with different types is impossible and/or a
bad idea, except for compiler writers having to deal with it? Does the
standard state WHY it isn't allowed.
regards
NPV
Jul 19 '05 #3
On Thu, 21 Aug 2003 13:40:18 +0300, Attila Feher wrote:
Nils Petter Vaskinn wrote:
On Thu, 21 Aug 2003 11:19:41 +0100, John Harrison wrote:
Its not allowed. Any expression has a single type, which can be
deduced at compile time by the compiler. Your suggestion would mean
that an expressions type could not be determined until a program runs.
Sounds like a nightmare to me (and not only for compiler writers).


And how would that be harder than compiling


Yes, it is. Your example can be decided at *compile* time.


But not which foo() to call.
Is there any reason that ?: with different types is impossible and/or a
bad idea,


Impossible.


Now that is plain unbelievable. If you said incredibly hard I would
believe you. If you said Impossible without rewriting the internals of the
compiler completely, I would believe you. But impossible is very rarely
the case.
except for compiler writers having to deal with it? Does the standard
state WHY it isn't allowed.


I am not going to look up the standard now. In C++ (well, why cannot
people read a decent C++ introductory book such as Accelerated C++
before starting to be critics? - sorry) expressions have a type. A
Type. One Type. And expression cannot have two types => be two things.


I did look ?: up in The C++ Programming Language and it provided very
little explanation. I also looked up the behaviour of ?: in K&R which
provided a little more detail and explained that the operands would have
to be the same type (unless one could be cast automatically to the other).

What I never found was any reason why this limitation exists in C++ (which
is much more dynamic than C), except for the "It's hard" explanation.

I didn't criticise anything that I'm aware of, I was asking "why is it
this way". (So that later I might descide to criticise if the explanation
doesn't make sense to me)

Now you have explained that it violates a principle (an expression has one
and only one type) I accept that. There never really was a problem since I
know how to do without ?: (if) I just wanted to know why the limitation
existed in the first place. (Though that principle might just be there to
make it easier for compiler writers)

regards
NPV
Jul 19 '05 #4
On Thu, 21 Aug 2003 14:52:36 +0300, Attila Feher wrote:
Nils Petter Vaskinn wrote:

I didn't criticise anything that I'm aware of, I was asking "why is it
this way". (So that later I might descide to criticise if the
explanation doesn't make sense to me)


You did not criticize? "Just because it may be hard for compiler writers
is no reason that it shouldn't be used, I expect the compiler writers
have solved problems much harder than that one." What was that then???


Which was my explanation why I didn't accept the "it's hard" explanation,
and expected there to be another reason. If you parsed that as "There is
something wrong with the C++ language" we have a bigger problem than me
wanting to know the reason behind one decision in the design of C++.

regards
NPV
Jul 19 '05 #5
NPV> If you parsed that as "There is something wrong with
NPV> the C++ language" we have a bigger problem than me
NPV> wanting to know the reason behind one decision in the
NPV> design of C++.

There might be something wrong with the C++ community. See
all those weird a-symmetrical beards around you? That's the
C++ crew.

You can use ?: this way (pragmatic, I cannot answer your question
why ?: is implemented the way it is)

void f(int a){}
void f(char a){}
true?f(1):f('1' );

-X
Jul 19 '05 #6

"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message > > foo(x ? "0" : 0, y ? "0" : 0);
Undefined, possibly a compiler warning. Or possibly really complex rules
for when ?: is allowed with different types. I guess this may be one of
the problems that made them decide against it (if they ever considered
it).


Disagree. The values of x and y are immaterial. The rules say that
the result of both the ?: expressions above is unambiguously char*.
Jul 19 '05 #7
On Fri, 22 Aug 2003 10:21:56 -0400, Ron Natalie wrote:

"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message > > foo(x ? "0" : 0, y ? "0" : 0);
Undefined, possibly a compiler warning. Or possibly really complex rules
for when ?: is allowed with different types. I guess this may be one of
the problems that made them decide against it (if they ever considered
it).


Disagree. The values of x and y are immaterial. The rules say that
the result of both the ?: expressions above is unambiguously char*.


Well yes, when I think about it, but I guess that was only meant to
illustrate the problem, replace the zeros with something else like 1.23
and you get the problem we're talking about.

NPV
Jul 19 '05 #8

"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message
Well yes, when I think about it, but I guess that was only meant to
illustrate the problem, replace the zeros with something else like 1.23
and you get the problem we're talking about.

In that case the program is ill-formed. There must be an unambiguous
type to that expression that does not depend on what the test argument
is.

Jul 19 '05 #9
"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message news:<pa******* *************** ******@spam.for .me.invalid>...
Why is it that if I have an overloaded function like:

void foo(int i);
void foo(char *c);

I can't do:

void bar() {
foo( condition ? 5 : "NULL");
}
As you might guess I'm building a query and my compiler (g++) complaines
about different operands to ?:

While I can easily replace the ?: with an if I was surprised that the
statement wasn't allowed. So I wondered if this really is specified in the
standard (or a quirk of g++) and there is any good reason for it to be
that way.


Yes. ?: is tricky.

In E0 ? E1 : E2, the expressions E1 and E2 must be converted to a common type.
The whole thing as an expression is supposed to have ONE type.
Imagine this code:

(condition ? A : B)->x;

The part "(condition ? A : B)" must have a single type known at compile time.

Generally, I guess this has to do with the parser: You expect the
unary expression (in the parentheses) to have a type. Otherwise
you could imagine that in nested such constructs a subexpression could
possibly have 16, 256 or even 65536 possible types!
Jul 19 '05 #10

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

Similar topics

6
7200
by: alg | last post by:
Is it possible to put different types of objects in a single STL list? If not what STL template should be used which will contains various objects of different types? Thanks for your help!
2
3638
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
4
3189
by: troloo | last post by:
Hello, I hope you can help me :)) The story goes as follows: I have a class with different methods and member variables. I store pointers to objects of this class inside a vector. Now, I would like to have a variable 'window' which could contain pointers to different objects (like 'class t1', 'class t2', etc.). I tried to use templates to create variable 'T* window' but I had some troubles later storing pointer to such a template-enabled...
2
2192
by: Che | last post by:
Greetings, I am writing an application that uses an extendible XML file. in order to validate that XML I use a main XSD and in additional - few more extensions XSD's that uses the types in the main XSD as base types. my clients can define their own XSD's extensions and use my generic application to process their XML's. The problem:
6
3241
by: Moshe Kravchik | last post by:
Hi all! I have 2 web services, one writtenin C++ (ATL) and another one in C#. Is there a way to define data stuctures in a single place both services could use? The structures are the same, but if I add a web reference from the C# project to the ATL project, all the structures belong to the different namespace and I have to manually copy them over and repeat the full namespace. Looking for an adivice, Moshe.
20
2641
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
3
2812
by: WaterWalk | last post by:
I read from c99 std TC2 community draft, and found the following statement in 6.7.2.3: "Tw o declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types. Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type." Then consider this common practice. Struct A type is defined in a
2
5120
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get promoted to "signed int": signed char
8
1864
by: Born Bugler | last post by:
What I'm actually talking about is, when you put the same class in different assemblies, you get two different types. This is reasonable (if you would call it) in most cases as it avoids possible misuse. However, what about if I do want to consider classes with exactly the same definition in different assemblies to be the same? I tried interpret_cast, but it works only for trivial scenarios like below: ref class ClassA { public:
15
3533
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
0
9456
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
9275
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
9872
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
9843
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,...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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
5142
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
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.