Skip to content

[clang]Should operands in ternary operator be allowed to have explicit copy constructor? #123700

Open
@Rush10233

Description

I've got a very confusing output with the following test:

struct QString
{
    QString()=default;
    explicit QString( const QString & );
};

QString def(){
    return QString{};
};

void foo( bool b, QString s )
{
     auto res = b ? def() : s;
}

It turns out that GCC accepts this with -std=c++17 or above; EDG and ICC always accept, and clang and MSVC always reject even with -std=c++17 or above: https://godbolt.org/z/zhza8Krfa

The diagnostic of clang and MSVC points out that if s is taken, the conversion from s to res will implicitly use the copy ctor of QString, which is not allowed:

<source>:13:29: error: no matching constructor for initialization of 'QString'
   13 |      auto res = b ? def() : s;
      |                             ^
<source>:4:14: note: explicit constructor is not a candidate
    4 |     explicit QString( const QString & );
      |              ^
<source>:3:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    3 |     QString()=default;
      |     ^
1 error generated.

Although the diagnostic sounds reasonable here, I still wonder if the standard has been made some changes since C++17 according to the behavior of GCC.

Metadata

Assignees

No one assigned

    Labels

    c++17clang:frontendLanguage frontend issues, e.g. anything involving "Sema"questionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions