Skip to content
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

In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed and the diagnostic should be reported. #92813

Open
romanova-ekaterina opened this issue May 20, 2024 · 0 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@romanova-ekaterina
Copy link
Contributor

Consider the following testcase (see below).

According to 13.3.1.7 Initialization by list-initialization is C++11 standard
"In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed."
However, clang doesn't report an error.

#include <initializer_list>
class AAA {
public:
    AAA(int,int,int) {
    }
};

class BBB {
    int value;
public:
    BBB() : value(10) {
    }

    BBB(const AAA& ) : value(20) {
    }

    explicit BBB(int,int,int) : value(33) {
    }

    int get_value()  {
	return value;
    }
};

int test_function (void) {
    BBB b({10,13,17}); // Diagnostic message should be reported here?
    return 1;
}

int main() { 
    test_function();
}

Clang doesn't report any diagnostic for it.

Microsoft's cl compiler (version 19) complains about it:

cl /c test.cpp /all
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30154 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9002 : ignoring unknown option '/all'
test.cpp
test.cpp(26): error C2664: 'BBB::BBB(BBB &&)': cannot convert argument 1 from 'initializer list' to 'BBB &&'
test.cpp(26): note: copy-list-initialization of 'BBB' cannot use an explicit constructor
test.cpp(17): note: see declaration of 'BBB::BBB'

Older gcc compiler (gcc 10.2.0) also complains about it:

$ gcc -c test.cpp -Wall
test.cpp: In function ‘int test_function()’:
test.cpp:26:21: error: call of overloaded ‘BBB(<brace-enclosed initializer list>)’ is ambiguous
   26 |     BBB b({10,13,17}); // Diagnostic message should be reported here?
      |                     ^
test.cpp:14:5: note: candidate: ‘BBB::BBB(const AAA&)’
   14 |     BBB(const AAA& ) : value(20) {
      |     ^~~
test.cpp:8:7: note: candidate: ‘constexpr BBB::BBB(const BBB&)’
    8 | class BBB {
      |       ^~~
test.cpp:8:7: note: candidate: ‘constexpr BBB::BBB(BBB&&)’
test.cpp:26:9: warning: unused variable ‘b’ [-Wunused-variable]
   26 |     BBB b({10,13,17}); // Diagnostic message should be reported here?
      |         ^
@romanova-ekaterina romanova-ekaterina added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

1 participant