#include <iostream>
#include <string>
using std::string;
struct BB
{
// generic cast
template<typename T>
operator T() const
{
std::cout<<"Generic cast\n";
return 0;
}
// string cast
operator string() const
{
std::cout<<"string cast\n";
return string("hello");
}
};
int main()
{
BB b;
string s ;
s= b;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBzdGQ6OnN0cmluZzsKc3RydWN0IEJCCnsKIC8vIGdlbmVyaWMgY2FzdAogdGVtcGxhdGU8dHlwZW5hbWUgVD4KIG9wZXJhdG9yIFQoKSBjb25zdAogeyAgIAogICBzdGQ6OmNvdXQ8PCJHZW5lcmljIGNhc3RcbiI7CiAgIHJldHVybiAwOwogfQoKIC8vIHN0cmluZyBjYXN0CiBvcGVyYXRvciBzdHJpbmcoKSBjb25zdAogeyAKICAgc3RkOjpjb3V0PDwic3RyaW5nIGNhc3RcbiI7CiAgIHJldHVybiBzdHJpbmcoImhlbGxvIik7CiB9Cn07CmludCBtYWluKCkKewogIEJCIGI7CiAgc3RyaW5nIHMgOwogIHM9IGI7Cn0=
prog.cpp: In function 'int main()':
prog.cpp:25:4: error: ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'BB')
s= b;
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:2950:7: note: candidate: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
operator=(const basic_string& __str)
^
/usr/include/c++/5/bits/basic_string.h:2958:7: note: candidate: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
operator=(const _CharT* __s)
^
/usr/include/c++/5/bits/basic_string.h:2969:7: note: candidate: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
operator=(_CharT __c)
^
/usr/include/c++/5/bits/basic_string.h:2985:7: note: candidate: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(std::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
operator=(basic_string&& __str)
^
/usr/include/c++/5/bits/basic_string.h:2997:7: note: candidate: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(std::initializer_list<_Tp>) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
operator=(initializer_list<_CharT> __l)
^