<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - "no viable conversion" error in brace initialization, compiles using assignment (g++ compiles both)"
   href="https://llvm.org/bugs/show_bug.cgi?id=30461">30461</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>"no viable conversion" error in brace initialization, compiles using assignment (g++ compiles both)
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.8
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>jamespharvey20@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Arch Linux
clang++ version 3.8.1 (tags/RELEASE_381/final)
Target: x86_64-unknown-linux-gnu
... Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1

Using "g++ --std=c++11", this code compiles and prints "Hello".

==========
#include <string>
#include <iostream>
using namespace std;

class someClass {
   public:
      operator const string() const { return "Hello"; }
};

int main() {
   someClass something;
   string str { something };   /* Generates an error using clang++ */
   cout << str << endl;
}
==========

Using "clang++ --std=c++11", this code fails compilation:

==========
clangNoViableConversion.cpp:12:17: error: no viable conversion from 'someClass'
to 'std::__cxx11::basic_string<char>'
   string str { something };
                ^~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/basic_string.h:397:7:
note: candidate
      constructor not viable: no known conversion from 'someClass' to 'const
std::__cxx11::basic_string<char> &' for 1st
      argument
      basic_string(const basic_string& __str)
      ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/basic_string.h:454:7:
note: candidate
      constructor not viable: no known conversion from 'someClass' to 'const
char *' for 1st argument
      basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
      ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/basic_string.h:476:7:
note: candidate
      constructor not viable: no known conversion from 'someClass' to
'std::__cxx11::basic_string<char> &&' for 1st argument
      basic_string(basic_string&& __str) noexcept
      ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/basic_string.h:503:7:
note: candidate
      constructor not viable: no known conversion from 'someClass' to
'initializer_list<char>' for 1st argument
      basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
      ^
clangNoViableConversion.cpp:7:7: note: candidate function
      operator const string() const { return "Hello"; }
      ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/basic_string.h:476:35:
note: passing
      argument to parameter '__str' here
      basic_string(basic_string&& __str) noexcept
                                  ^
1 error generated.
==========

Changing the offending line from

==========
   string str { something };
==========
to
==========
   string str = something;
==========

Makes clang++ compile a working program.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>