samedi 23 juillet 2016

Why does it not work to overload "operator <<" on std::string?

#include <string>
#include <type_traits>

using namespace std;

template
<
    typename CharT,
    template<typename> class Traits,
    template<typename> class Allocator,
    typename RightT,
    typename StringT = basic_string
    <
    CharT,
    Traits<CharT>,
    Allocator<CharT>
    >
>
enable_if_t
<
    is_constructible<StringT, RightT>::value,
    StringT&
>
operator <<(StringT& lhs, const RightT& rhs)
{
    return lhs.append(rhs);
}

int main()
{
    string s1, s2;
    s1 << s2; // compilation error!

    return 0;
}

My compiler is VS 2015 Update 3. The compilation error message is:

error : invalid operands to binary expression ('string' (aka 'basic_string, allocator >') and 'string')

Why does it not work as expected?

Aucun commentaire:

Enregistrer un commentaire