10

Fuck c++
Everytime I have to use this fucking language it spits up some other bullshit error

`cannot convert std::vector<int>() to std::vector<int>&`

WHY THE FUCK ARE STACK ALLOCATED VALUES EVEN THEIR OWN TYPES

AND WHY CANT I TAKE A GOD DAMN REFERENCE TO IT

JUST WHYYYYY

Comments
  • 1
    What went wrong? It should work.
  • 2
    @mr-user Turns out `std::vector<int> bullshit;` isn't the same as `std::vector<int> bullshit();`

    The former works, the latter is a function prototype. Because that makes fucking sense and is at all consistent or intuitive

    Man fuck this language
  • 3
    You can't take a reference to temporary. It will be discarded at the compilers convenience and any reference to that object will be invalid. This is an object lifetime issue.
  • 2
    @12bitfloat Oh, so it is something else. Ah okay.
  • 1
    i've had this exact issue i don't remember how many goddamn times and every time it makes me actually scream
  • 1
    @Parzi I always forget about it and then think "Hey, I like to be explicit so let me just put these empty parentheses here to really make it clear I want to call the default constructor"
  • 0
    I mean, a type is not a constructor eh?

    I mean, no idea bout C
  • 1
    @Ranchu That's what confused me. `std::vector<int>()` isn't some special default constructed type of std::vector nor is it even an std::vector. It's actually a function type without args that returns an std::vector

    It does make sense in a way but it's still stupid
  • 3
    std::vector<int> vtype = std::vector<int>();

    std::vector<int>& vref = vtype; // works

    std::vector<int>& vref2 = std::vector<int>(); // does not compile, trying to ref a temporary
  • 1
    @Demolishun That makes sense, but what I was doing is this:

    // Works, default constructed vector
    std::vector<int> list;

    // Doesn't work, function prototype declaration
    std::vector<int> list();

    // Works, vector with initial capacity
    std::vector<int> list(10);

    Now that's pretty dumb. It of course makes sense and I (now) know why it does it but that's still baaaaad design
  • 4
    @12bitfloat Oh, that is why they added {} for that now.

    std::vector<int> list{};
  • 1
    I second his opinion @12bitfloat , try to use {} unless you are working with older C++ (which I think you shouldn't have any reason to work with it since it is backward compatible)
  • 0
    @FrodoSwaggins

    I don't know that C++ is not backward compatible. I thought that in C++ the older thing will still work when you upgrade to newer version.

    Could you give me an example of what kind of thing are not backward compatible?

    I heard that the C++ is having trouble since it cannot remove stuff since it need to maintain backward compatibility (at least that what I got from CppCon).
  • 0
    @FrodoSwaggins

    That make sense that they kill it (even if it break the compatibility issue)
  • 0
    @FrodoSwaggins lmao
  • 0
    @mr-user @Demolishun I completely forgot about aggregate initializers. Do note that they don't call constructors (as far as I know) so you still have the inconsistent syntax of `std::vector a{};` vs `std::vector b(10);`
  • 2
    @FrodoSwaggins

    So in c++98 you could shoot your dick off.

    In c++11 removed this ability, but probably added other parts that could be shot off.
  • 0
    @Demolishun

    C++ 11 mostly add new feature which replace older dangerous feature.
  • 1
    @FrodoSwaggins Yeah that's what I'm saying, aggregate initializers don't call direct constructors of the initialized object, they initialize the object and it's fields themselves. So you can't explicitely call the default constructor for a stack allocated object of a class because syntax ambiguity
  • 2
    @12bitfloat Yeah, probably better to use none for default arg version.
  • 0
    I dunno man, C++ is a really complex language. I am always paranoid when I start studying it because I want to make sure that If I fuck up it is because of my own damn self. The language belongs to masters really and just like Javascript fucking shit up left and right having you learn all sorts of workarounds with quirks the same applies to C++, you REALLY need to know your shit to work with C++, yes, this includes keeping up with the standards present in the compilers.
  • 1
    @FrodoSwaggins precisely why I say that its a language for masters man, y'all keep on the craft of an already complex language and learn the new standards and its hard already as it is. I appreciate cpp becay it sorts of weeds out the good developers from the bad more than any other language.
  • 0
    @FrodoSwaggins No there actually is ambiguity. You would expect `std::vector<int> a();` to default construct a vector but it doesn't... because it's ambiguous

    (Yes, I'm aware that it's not *technically* ambiguous, just inconsistent, but same same only different)
  • 0
    @FrodoSwaggins If I had a hard time with minor syntax ambiguities I wouldn't be a programmer, trust me

    What I can't get over is the seemingly arbitrary inconsistency. It's just bad language design. I guess that's all I'm trying to say
  • 2
    @AleCx04 Heh, you make me feel good about me today!
  • 3
    Can recommend:

    https://amazon.com/C-Programming-La...

    Also, this quote is apt:

    "Within C++, there is a much smaller and cleaner language struggling to get out."

    A later clarification adds, "And no, that smaller and cleaner language is not Java or C#."
  • 1
    @Demolishun Without a doubt C++ is a great -- and probably the most powerful ever -- language. It's just that as it grew over the last decades it now has so many warts and so many dumb syntactic/semantic idiosyncrasies that I don't see a way of cleaning it up other than throwing it away and starting from a clean state. And that's what Rust is to me: Reimagined C++

    (Yeah you thought I wouldn't bring up Rust in a thread about systems languages, huh? :P)
  • 1
    @12bitfloat

    see that coming mile away. Rust always come up when discussing about system language.
  • 1
    @Demolishun I have mad respect for anyone that successfully builds a career in what I consider the hardest language to master. I know only the side of web development and data analysis, but our tools in both areas are so far removed from the metal that for some of us the idea of manually dealing with memory, the most important resource you can manipulate in a computer, is just a far fetched idea. It doesn't help that most of us touched C or C++ in academia, for which we were definitely not expecting to fight the language so much in regards to not learning the inner models and syntax to be profficient. Me? I liked it, a fucking lot, but still went the web side and I don't use C++ for anything. I have been following the Cherno on youtube in an effort to use C++ in the game design side of things. Its hard, but I love it, eventually I want to transition to working in something with C++ full time.
Add Comment