Skip to content

[clang++] Clang fails to call initializer_list constructor in specific cases #89079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidkjaer opened this issue Apr 17, 2024 · 4 comments
Closed
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate

Comments

@davidkjaer
Copy link

There seems to be a very specific case where clang does not call an initializer_list constructor even though it should apply.
The issues arises when the class Foo has an std::initializer_list<Foo> constructor and you create an object like thisFoo f{Foo{}};:

#include <initializer_list>
#include <iostream>
#include <vector>

class Foo
{
public:
    Foo()
    {
    }

    explicit Foo(std::initializer_list<Foo> l)
    {
        std::cout << "Initializer list constructor\n";
    }

    void hi()
    {
        std::cout << "Hello\n";
    }
};

int main() {
    Foo other;
    Foo f{std::move(other)};
    f.hi();
}

When compiled with clang 18.1 the above example prints:
Hello

On both gcc and msvc the example prints:
Initializer list constructor
Hello

If I switch the initialization to Foo f{{std::move(other)}} or Foo f{Foo{}, Foo{}} clang correctly calls the initializer_list constructor.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Apr 17, 2024
@efriedma-quic
Copy link
Collaborator

Could you explain why you think gcc is right here? I'm not an expert on overload resolution, but at first glance, Foo(Foo&&) should rank higher than Foo(std::initializer_list<Foo>) given the extra conversion required to construct the std::initializer_list.

@efriedma-quic efriedma-quic added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Apr 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 17, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (davidkjaer)

There seems to be a very specific case where clang does not call an initializer_list constructor even though it should apply. The issues arises when the class `Foo` has an `std::initializer_list<Foo>` constructor and you create an object like this`Foo f{Foo{}};`:
#include &lt;initializer_list&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

class Foo
{
public:
    Foo()
    {
    }

    explicit Foo(std::initializer_list&lt;Foo&gt; l)
    {
        std::cout &lt;&lt; "Initializer list constructor\n";
    }

    void hi()
    {
        std::cout &lt;&lt; "Hello\n";
    }
};

int main() {
    Foo other;
    Foo f{std::move(other)};
    f.hi();
}

When compiled with clang 18.1 the above example prints:
Hello

On both gcc and msvc the example prints:
Initializer list constructor
Hello

If I switch the initialization to Foo f{{std::move(other)}} or Foo f{Foo{}, Foo{}} clang correctly calls the initializer_list constructor.

@EugeneZelenko EugeneZelenko removed the clang Clang issues not falling into any other category label Apr 17, 2024
@efriedma-quic
Copy link
Collaborator

Actually, digging a bit more, this might be a duplicate of #24186 ?

@davidkjaer
Copy link
Author

Yes. That definitely looks like a duplicate.

I have deleted my previous comment since I had missed the word "aggregate" making my conclusion incorrect. No need to leave misinformation on the internet :)

@EugeneZelenko EugeneZelenko added the duplicate Resolved as duplicate label Apr 18, 2024
@EugeneZelenko EugeneZelenko closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

4 participants