Skip to content

Imprecise translation for ternary operator #324

@lSoleyl

Description

@lSoleyl

Hi,

this is a great tool for crosschecking the compiler's interpretation of C++ code. However the expansion of the ternary operator looks a bit misleading to me. Given the following code:

struct Str {
  Str(const char* string) : string(string) {}
  
  operator const char*() const { 
    return string; 
  }
  
  const char* string;
};

Str globalString = "test";


const char* getString(bool empty) {
  return empty ? "" : globalString;
}

With C++17 the getString() function gets translated into:

const char * getString(bool empty)
{
  return static_cast<const char *>(empty ? Str("") : Str(globalString).operator const char *());
}

I would argue that a parenthesis is missing and it should look like this for both result types of the ternary expression to match:

const char * getString(bool empty)
{
  return static_cast<const char *>((empty ? Str("") : Str(globalString)).operator const char *());
}

Also when setting the compilation option to anything below C++17, then the result looks even weirder:

const char * getString(bool empty)
{
  return static_cast<const char *>(empty ? Str(Str(Str(""))) : Str(globalString).operator const char *());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions