I
Ian Collins
While "porting" some existing code to g++, I ran into a problem with a
generic value class that uses a template conversion operator to return
values. Here's a stripped down version:
#include <string>
struct Value
{
Value() {}
template <typename T> operator T() { return T(); }
};
int main()
{
Value v;
int n(v);
std::string s(v);
}
My original compiler is happy with the string initialisation, but g++
whinges about the conversion being ambiguous (edited to cut the noise):
example.cc:15: error: call of overloaded 'basic_string(Value&)' is ambiguous
candidates are:
std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*,
const _Alloc&)
std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
std::basic_string<_CharT, _Traits, _Alloc>&)
std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&)
But it will happily accept
std::string s = v;
So my question is, which compiler is correct?
generic value class that uses a template conversion operator to return
values. Here's a stripped down version:
#include <string>
struct Value
{
Value() {}
template <typename T> operator T() { return T(); }
};
int main()
{
Value v;
int n(v);
std::string s(v);
}
My original compiler is happy with the string initialisation, but g++
whinges about the conversion being ambiguous (edited to cut the noise):
example.cc:15: error: call of overloaded 'basic_string(Value&)' is ambiguous
candidates are:
std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*,
const _Alloc&)
std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
std::basic_string<_CharT, _Traits, _Alloc>&)
std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&)
But it will happily accept
std::string s = v;
So my question is, which compiler is correct?