« first day (2326 days earlier)      last day (533 days later) » 

8:42 AM
@Mgetz wdym?
 
 
3 hours later…
11:53 AM
see the linkage section
 
 
1 hour later…
1:05 PM
Is it possible/legal to overload the operator<< to assign to an object whose type is a struct?
Sorry for my poor English, a simple code snippet is clearer than words.
#include<iostream>
#include<vector>

std::vector<int>& operator<<(std::vector<int> &vec, int num)
{
vec.push_back(num);

return vec;
}


int main()
{
std::vector<int> vec{1,2,3};

vec<<4;
vec<<5;

for(auto val:vec)
{
std::cout << val << std::endl;
}
}
My question should be wether it is possible/legal to overload the operator<< to append an object to a container.
 
1:23 PM
it's possible and legal, and used in some popular frameworks
not sure if it's a great idea though
 
1:46 PM
I generally wouldn't recommend it as it can cause weird and odd interactions. It's generally considered a mistake that Iostreams did it.
 
@PeterT Any suggestion is welcome.
 
why do you want to? it just obfuscates what's going on
 
@Mgetz I see. What I intend to do is to make a uniform function to append both std::vect<int> and int to another std::vector<int>,say:
std::vector<int> all;
all << 1;
std::vector<int> part{1,2,4}
all<<part;
 
why do you need to make a Domain specific language (DSL) for this?
 
depends, does it need to be extending std::vector itself? If not, what about a free-function instead?
Stuff like QVector have an append function overload with both single elements and vectors https://doc.qt.io/qt-5/qvector.html
 
1:53 PM
See this code snippet to better understand my goal.
 
I get what you're doing... I just don't' get why you're doing it. Operator overloading is generally considered a really bad thing
void append_to(std::vector<int> &vec, const std::vector<int>& vec_partial)
{
    vec.reserve(vec.size() + vec_partial.size());
    for(auto val:vec_partial)
    {
        vec.push_back(val);
    }

}
vec.insert(vec.end(), vec_partial.begin(), vec_partial.end());
or the even cleaner vec.insert(vec.end(), { 1, 2, 3, 4});
 
2:21 PM
I see, thank you so much.
 
also consider what if I wanted to do a mathematical operator << for various container types with integer internals? Where << left shifts every element by some amount?
that would create a case of ambiguity where it can't decide which one to use. The one that makes math sense or the one that is just a DSL
hence generally speaking... operator overloading is bad
 
 
5 hours later…
7:03 PM
@Mgetz While I get your point, that's overstating the situation drastically. Overloading operators in ways that aren't obvious can be bad. But there's nothing wrong with overloading operators for things like mathematical operations for a big integer class (for a particularly obvious example).
 
hence "Generally speaking"
 
8:00 PM
@Mgetz And I'll repeat: that's drastically overstating the situation. Generally speaking, your claim is dead wrong. While it's certainly true that with sufficiently poor judgement, you can make a real mess with operator overloading, experience with Java (for only one example) indicates that lack of operator overloading is drastically worse.
The big shortcoming with C++ is that you're stuck with overloading only existing operators, and using their existing precedence/associativity. ML and its descendants let you specify operators with entirely new names, and specify their precedence and associativity, which works out extremely well (but does have a few limitations of its own, such as requiring that you specify all the overloads of a particular operator together).
 
8:35 PM
@JerryCoffin I'll respectfully not only disagree but say you're dead wrong. Operator overloading incorrectly causes all sorts of damage to the point that most languages go out of their way to ban it in design. BTW the only reason I'm being this rude... is turn about is fair play. Calling me dead wrong is extremely disrespectful.
Also I don't care what ML does, this is C++
 
 
2 hours later…
11:04 PM
@Mgetz Seems like you need to make up your mind. On one hand, your directly cite "most languages", as evidence for your claim, but on the other you decide to ignore ML, even though it's one of those "most languages", and one of the more highly respected languages around. The fact that Brainfuck, COBOL, Malbolge, and Microsoft BASIC all avoid operator overloading doesn't tell us anything about what well designed languages do.
 

« first day (2326 days earlier)      last day (533 days later) »