Compiler error for conditional operator “?:” when used with typecasting operator

前端 未结 3 1271
囚心锁ツ
囚心锁ツ 2021-02-14 19:43

Following code is in simplest form:

struct X {
  operator char () const { return \'a\'; }
};

int main ()
{
  X obj, *p = &obj;
  char a = *p;  // ok
  char          


        
3条回答
  •  爱一瞬间的悲伤
    2021-02-14 20:32

    There are issues with implicit conversion between classes for the conditional operator ?: in certain versions of G++. Also depending on the version you may either see an error, or you may /not/ see such an errors, however all three of your samples are correctly flagged by G++.

    This is less of an answer to the original question but a re-certification of the original poster's, and others', qualm that there might be problems with spurious errors with G++ compiler and the conversion behavior for class types as specified in ISOC++:2003 5.16/3 [expr.cond]/3 (also outlined above by @Nawaz)

    One example that shows the converse of the original poster's sample where implicit conversion in both directions can happen but isn't flagged can be seen here.

    I compiled this on G++ v3.4.6, v4.1.2, v4.2.1 and v4.5.0(windows) and saw no problems because as again @Nawaz said this is the correct behavior within the ISOC++:2003 spec.

    However, when I followed the link on your IDEONE page the structure definition was different and that is the root of your error.

    struct X
    {
      char ch;
      X(const char c) : ch(c) {}
    
      operator char () const { return ch; }
    };
    
    int main ()
    {
      X obj('a'), *p = &obj;
      char a = *p; // ok
      char c = (true)? *p : 'b';
    }
    

    Then as above the struct is different and I do get the error your see. This is a correct error as 'z' can be converted into X or char, and because of the constructor 'z' can also be converted into X - bi-directionality of X/'z' so the compiler should stop. However with a little change it can be made to pass and that is exactly the same bug as the GNU G++ bug posted above.

    If you convert your sample above to a pointer sample it will fail. Character pointer, int pointer or whatever.

    Although unrelated I thought it might be a good point to highlight something that often caught me out whilst cross-porting between Windows/Linux. MSVC will happily pass such conversions often if it deems the conversion to be "safe", or often it may proffer a warning that conversion is being done at the risk of loss of precision - I am not sure what the trigger for allowing this is.

提交回复
热议问题