mercredi 27 avril 2016

Operator comma overloading

I'm trying to learn more about how operator overloading works.

I understand that overloading the comma operator may not be the best idea, but this is for instructional purposes only.

I'm expecting the following code to use my overloaded operator (I used brackets as I'm aware the comma operator has the lowest precedence) to construct a vector containing (1,2) and then call the vector's assignment operator.

However, I get an error:

no known conversion from argument 1 from 'int' to 'const std::vector<int>&'

I don't understand why this is happening. (1,2) should construct a vector so it shouldn't be trying to convert from an int to a vector<int>?

#include <vector>
#include <utility>

using std::vector;
using std::move;

template <typename T>
vector<T> operator,(const T& v1, const T& v2)
{
    vector<T> v;
    v.push_back(v1);
    v.push_back(v2);
    return move(v);
}

int main()
{
    vector<int> a;
    a = (1,2);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire