Overloaded operator question

W

woodbrian77

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::eek:perator<<(T) [with T = char*]’:
cmwAmbassador.cc:309:44: required from here
../ErrorWords.hh:47:40: error: call of overloaded ‘to_string(char*&)’ isambiguous
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
 
B

bblaz

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::eek:perator<<(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
 
W

woodbrian77

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top