Overloaded operator question

Discussion in 'C++' started by woodbrian77@gmail.com, Jan 15, 2014.

  1. Guest

    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
    , Jan 15, 2014
    #1
    1. Advertising

  2. bblaz Guest

    On 01/15/14 22:47, wrote:
    > 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
    bblaz, Jan 16, 2014
    #2
    1. Advertising

  3. Guest

    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
    , Jan 18, 2014
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dodo
    Replies:
    1
    Views:
    352
    Ron Natalie
    Aug 26, 2003
  2. John Goche
    Replies:
    2
    Views:
    331
    Frederick Gotham
    Sep 4, 2006
  3. Afshin
    Replies:
    0
    Views:
    282
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    283
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    409
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page