[llvm-bugs] [Bug 30461] New: "no viable conversion" error in brace initialization, compiles using assignment (g++ compiles both)

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Sep 20 03:39:56 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=30461

            Bug ID: 30461
           Summary: "no viable conversion" error in brace initialization,
                    compiles using assignment (g++ compiles both)
           Product: clang
           Version: 3.8
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jamespharvey20 at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160920/1722bb61/attachment.html>


More information about the llvm-bugs mailing list