sqlpp17 icon indicating copy to clipboard operation
sqlpp17 copied to clipboard

order of evaluation problem

Open CHChang810716 opened this issue 5 years ago • 1 comments

Hi,

I found there are some code like:

struct
{
  bool first = true;
  [[nodiscard]] auto to_string() -> std::string
  {
    if (first)
    {
      first = false;
      return "";
    }
    else
    {
      return ", ";
    }
  }
} separator;

return (std ::string{} + ... + (separator.to_string() + to_sql_column_spec_string(context, ColumnSpecs{})));

https://github.com/rbock/sqlpp17/blob/6f7a0f485a37adfd755f627bd98ab3721b498cb4/connectors/postgresql/include/sqlpp17/postgresql/clause/create_table.h#L84

Such code might be undefined behavior due to the order of evaluation

By the standard, the first separator("") could be introduced to any place between the columns. For gcc, the empty separator is place to the back of the line.

Here is a demo of such bug on ideone.

Thanks,

CHChang810716 avatar Jan 07 '21 19:01 CHChang810716

Thanks for the report. I obviously got confused with some of the new rules.

It seems I could use the comma operator in a fold expression instead.

rbock avatar Jan 08 '21 06:01 rbock