Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloaded operator question

69 views
Skip to first unread message

woodb...@gmail.com

unread,
Jan 15, 2014, 4:47:29 PM1/15/14
to
Notice the operator<< functions here:

class failure : public ::std::exception {
::std::string whatStr;

public:
explicit failure (char const* what_) : whatStr(what_)
{}

explicit failure (::std::string what_) : whatStr(::std::move(what_))
{}

~failure () throw()
{}

char const* what () const throw()
{ return whatStr.c_str(); }


// failure& operator<< (char* s)
// {
// whatStr.append(s);
// return *this;
// }

failure& operator<< (char const* s)
{
whatStr.append(s);
return *this;
}

failure& operator<< (::std::string const& s)
{
whatStr.append(s);
return *this;
}

template <class T>
failure& operator<< (T val)
{
whatStr.append(::std::to_string(val));
return *this;
}
};

If I add back the commented out operator<< above the
following line is accepted by the compiler:

if(argc!=2) throw failure("Usage: ")<<*argv<<" config-file-name";

But if it is stays commented out, I get an error:

/ErrorWords.hh: In instantiation of ‘cmw::failure& cmw::failure::operator<<(T) [with T = char*]’:
cmwAmbassador.cc:309:44: required from here
./ErrorWords.hh:47:40: error: call of overloaded ‘to_string(char*&)’ is ambiguous
whatStr.append(::std::to_string(val));


I'd like to be able to remove the commented out version
of that operator from the class. I guess the compiler
prefers the function template version to the version that
takes a char const*. Any ideas? Thanks.

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

bblaz

unread,
Jan 16, 2014, 4:11:01 AM1/16/14
to
> /ErrorWords.hh: In instantiation of �cmw::failure& cmw::failure::operator<<(T) [with T = char*]�:
> cmwAmbassador.cc:309:44: required from here
> ./ErrorWords.hh:47:40: error: call of overloaded �to_string(char*&)� is ambiguous
> whatStr.append(::std::to_string(val));
>
>
> I'd like to be able to remove the commented out version
> of that operator from the class. I guess the compiler
> prefers the function template version to the version that
> takes a char const*. Any ideas? Thanks.
>
> Brian
> Ebenezer Enterprises - In G-d we trust.
> http://webEbenezer.net
>


Templated version is selected because it is an exact match, and none of
the other candidates are more specialized.

So, either
take argv as const char**,

or const cast
if(argc!=2) throw failure("Usage: ")<<const_cast<const char*>(*argv)<<"
config-file-name";


or modify the templated function, i.e.

template <class T, class = typename std::enable_if<!std::is_same<T,
char*>::value>::type>

failure& operator<< (T val) {
whatStr.append(std::to_string(val));
return *this;
}

blaz

woodb...@gmail.com

unread,
Jan 18, 2014, 12:20:08 PM1/18/14
to
On Thursday, January 16, 2014 2:11:04 AM UTC-6, bblaz wrote:
>
>
> Templated version is selected because it is an exact match, and none of
> the other candidates are more specialized.
>
> So, either
>
> take argv as const char**,
>

I have at least one other place where I run into this
problem and that has to do with some code generated
by flex.

>
>
> or const cast
>
> if(argc!=2) throw failure("Usage: ")<<const_cast<const char*>(*argv)<<"
> config-file-name";
>
>
> or modify the templated function, i.e.
>
> template <class T, class = typename std::enable_if<!std::is_same<T,
> char*>::value>::type>
>
> failure& operator<< (T val) {
> whatStr.append(std::to_string(val));
> return *this;
> }
>


I tried this and it works fine as far as I can tell.
But I don't really like it either. It doesn't seem
like much of an improvement over the original state

Brian
Ebenezer Enterprises - I will lift up my eyes to the mountains.
http://webEbenezer.net
0 new messages