[Bug c++/91742] User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 --- Comment #9 from Diego Franco --- Correction/editing of last section of former comment: ... - init brace does not work for user defined conversion reference WITHOUT static cast: A a {}; const std::vector& b {a}; // changed from "auto" to "std::vector" assert( == ); // does not work ... ...
[Bug c++/91742] User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 --- Comment #8 from Diego Franco --- So to summarize, these are the main reason why I believe this should be addressed: - init brace works for references of any type: std::vector a {}; std::vector& b {a}; assert( == ); // works int c {}; int& d {c}; assert( == ); // works - init brace works for user defined conversion reference when using static cast: class A { operator const std::vector&() const {return a_;} std::vector a_; }; A a {}; const auto& b {static_cast&>(a)}; assert( == ); // works - init brace does not work for user defined conversion reference WITHOUT static cast: A a {}; const auto& b {a}; assert( == ); // does not work I think the above behavior is quite unexpected, and does not follow any logic of other behaviors in the language. I found this unexpected behavior by running unit tests. Also, semantic wise is correct to use user-defined-conversion without static cast (above example).
[Bug c++/91742] User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 --- Comment #7 from Diego Franco --- This works: #include #include struct A { operator const int&() const { return a_; } int a_; }; int main() { A a {}; const auto& b1 {static_cast(a)}; const int& b2 {a}; assert(_ == ); assert( == ); // works
[Bug c++/91742] User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 --- Comment #6 from Diego Franco --- Also the brace initialization works with primitive types for the code I posted in the first place. That's definitely a code smell.
[Bug c++/91742] User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 --- Comment #4 from Diego Franco --- > It's not implementation defined, but I think all implementations are required > to do what GCC 8 does (and other compilers agree). It worked on linaro 7.4.1 gcc with c++17, gcc 7.1.0 with c++17 only. > If "const X& b2 (a)" is used instead of list-init, the second assertion > passes. That seems like an issue no working with {}.
[Bug c++/91742] User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 --- Comment #1 from Diego --- The code does work when changing std::vector for any primitive types, i.e. int.
[Bug c++/91742] New: User defined conversion references
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91742 Bug ID: 91742 Summary: User defined conversion references Product: gcc Version: 8.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: di...@franco-technologies.com Target Milestone: --- The following code does not work as intended: //Example- #include #include struct A { operator const std::vector&() const { return a_; } std::vector a_; }; int main() { A a {}; const auto& b1 {static_cast&>(a)}; const std::vector& b2 {a}; assert(_ == ); assert( == ); // does not work with gcc 8.3.0 any standard // works with gcc 7.1.0 with c++17 only } //End of example- I read through the c++17 standard and did not find any information about this behavior being implementation defined. Diego