Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Parameter mismatch with ?: operator

Reply
Thread Tools

Parameter mismatch with ?: operator

 
 
Old Wolf
Guest
Posts: n/a
 
      08-30-2005
My compiler rejects this:

std::string foo()
{
std::string s;
return some_condition() ? s : "";
}

saying that the last two operands of ?: must be the same type.
I have to fix it by changing "" to std::string() .

Is this correct behaviour? I remember reading that the compiler
ought to try implicitly converting one of the operands to the
type of the other.

 
Reply With Quote
 
 
 
 
upashu2
Guest
Posts: n/a
 
      08-30-2005
>std::string foo()
{
std::string s;
return some_condition() ? s : "";
}
>

My Compiler is not showing any problem : gcc 3.4.2, vc++7.1

>saying that the last two operands of ?: must be the same type.

Absolutely right.
> I have to fix it by changing "" to std::string() .

It should be implicit conversion.Most probably your compiler either
don't support it or it has a option for it and that option is turned
off.

 
Reply With Quote
 
 
 
 
Gabriel
Guest
Posts: n/a
 
      08-30-2005
> My compiler rejects this:

What compiler do you use?
 
Reply With Quote
 
Torsten Mueller
Guest
Posts: n/a
 
      08-30-2005
"Old Wolf" <(E-Mail Removed)> schrieb:

> std::string foo()
> {
> std::string s;
> return some_condition() ? s : "";
> }
>
> saying that the last two operands of ?: must be the same type. I
> have to fix it by changing "" to std::string() .
>
> Is this correct behaviour? I remember reading that the compiler
> ought to try implicitly converting one of the operands to the type
> of the other.


I would also suggest to fix this as you describe it yourself.
Otherwise the compiler looks for an implicit constructor matching a
const char* parameter. If your code is compiled with a STL
implementation using explicit constructors the compiler will not use
even a matching constructor.

I find implicit constructors bad style because people beeing not
familiar with this (especially beginners) don't even know that there's
a constructor and from what class. It's the same with function
parameters.

T.M.
 
Reply With Quote
 
Wolfgang Forstmeier
Guest
Posts: n/a
 
      08-30-2005
In my opinion the compiler is right,
because you had to return an std::string
and "" is not a std::string.

Greetings Wolfgang...


Old Wolf wrote:
> My compiler rejects this:
>
> std::string foo()
> {
> std::string s;
> return some_condition() ? s : "";
> }
>
> saying that the last two operands of ?: must be the same type.
> I have to fix it by changing "" to std::string() .
>
> Is this correct behaviour? I remember reading that the compiler
> ought to try implicitly converting one of the operands to the
> type of the other.
>

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-30-2005
Gabriel wrote:
> > My compiler rejects this:

>
> What compiler do you use?


Borland C++Builder 5 (compiler version 5.5.1).
It looks like it's just a compiler bug then -- the library
(Rogue Wave 2.1.1) doesn't use explicit constructors.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
without declare parameter [double square(parameter)] return 0 in main WanHongbin@gmail.com C Programming 5 10-01-2008 03:31 AM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM
Parameter / argument mismatch in template scenario Dave C++ 2 04-05-2004 10:32 PM
java.io.BufferedWriter and java.io.PrintStream - a seeming parameter/argument mismatch in method invocation Novice Java 1 07-19-2003 06:46 PM



Advertisments