Parameter mismatch with ?: operator

Discussion in 'C++' started by Old Wolf, Aug 30, 2005.

  1. Old Wolf

    Old Wolf Guest

    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.
     
    Old Wolf, Aug 30, 2005
    #1
    1. Advertising

  2. Old Wolf

    upashu2 Guest

    >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.
     
    upashu2, Aug 30, 2005
    #2
    1. Advertising

  3. Old Wolf

    Gabriel Guest

    > My compiler rejects this:

    What compiler do you use?
     
    Gabriel, Aug 30, 2005
    #3
  4. "Old Wolf" <> 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.
     
    Torsten Mueller, Aug 30, 2005
    #4
  5. 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.
    >
     
    Wolfgang Forstmeier, Aug 30, 2005
    #5
  6. Old Wolf

    Old Wolf Guest

    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.
     
    Old Wolf, Aug 31, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian douglas
    Replies:
    0
    Views:
    1,837
    ian douglas
    Aug 19, 2003
  2. Raj
    Replies:
    6
    Views:
    1,786
    Scott M.
    Feb 15, 2006
  3. Owin_Wan_Kenobi

    asp.net version mismatch????

    Owin_Wan_Kenobi, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    402
    Owin_Wan_Kenobi
    Oct 29, 2003
  4. Novice
    Replies:
    1
    Views:
    1,182
    Mike Schilling
    Jul 19, 2003
  5. Dave
    Replies:
    2
    Views:
    445
    Martin Eisenberg
    Apr 5, 2004
Loading...

Share This Page