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
  • 2
    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.
  • 1
    @12bitfloat Oh, so it is something else. Ah okay.
  • 7
    Not defending C++, but what you've run into is the 'most vexing parse': https://en.wikipedia.org/wiki/...

    Demo and fix in Godbolt (just change the `#if 0`s to `#if 1`s to see the failures): https://godbolt.org/z/k29kx8
  • 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
  • 2
    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
  • 3
    @12bitfloat Oh, that is why they added {} for that now.

    std::vector<int> list{};
  • 3
    What can I say. Some people have a tough time writing c++ that compiles and some don’t. Shrug
  • 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)
  • 2
    @mr-user c++ standards are not backwards compatible. There are things you can do with standard vector that compile in c++ 98 that don’t compile in 11.
  • 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).
  • 1
    @mr-user const iterators. You can iterate over a const vector without using a const iterator in c++98. That’s wrong in c++11
  • 0
    @FrodoSwaggins

    That make sense that they kill it (even if it break the compatibility issue)
  • 0
  • 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);`
  • 1
    @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.
  • 0
    @Demolishun yes and no. So const guarantees are paper thin in c++ because of side effect modeling. That’s why const cast works in c++ and don’t even get me started about how sad that makes me.

    You can have a const method in a class but have that call into another class that has a non const pointer to you. And then gangbusters, nothing is const, so even when calling const methods, the compiler doesn’t generate any special liveness rules. Unlimited side effects are possible if you can’t inline.
  • 0
    @12bitfloat this is I won’t say false but patently misleading. Aggregate initializations of classes “don’t call constructors” because the class is required to not have user defined constructors and thus default constructors can be inferred and inlined. Members of the class will still have their default constructor called.

    You cannot instantiate a struct or class in c++ without either inferring, Inlining, or calling a constructor.
  • 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
  • 1
    @12bitfloat Yeah, probably better to use none for default arg version.
  • 0
    @12bitfloat there’s no ambiguity at all.

    std::vector<int> x;

    Is an explicit call to the default (no arguments) constructor.
  • 1
    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
    @AleCx04 I think keeping up with the standards is what makes what you’re saying true. There’s crazy shit coming in c++20 that I’m not excited for and don’t plan on using, but I’m still going to have to debug and review other people’s shitty code and so I’m forced to keep up anyway.
  • 2
    @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
    @12bitfloat all I can say is I don’t really have a tough time with this one.
  • 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
  • 1
    @AleCx04 Heh, you make me feel good about me today!
  • 1
    @12bitfloat well have to agree to disagree. C++ has issues but this isn’t one of them.
  • 2
    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