/dpt/ - Daily Programming Thread

Teach me C++ edition.

Previous thread: What are you working on Sup Forums?

Other urls found in this thread:

humblebundle.com/books/python-by-packt-book-bundle
my.mixtape.moe/kiyaun.webm
cdecl.org/
neuralnetworksanddeeplearning.com/
twitter.com/NSFWRedditGif

c++ is the greatest language of all time

First for reduction contexts

Can anyone explain in simple terms to a brainlet like myself what l-values and r-values are? I've tried to understand them but I don't really understand what they are or what they are used for.

l-values are stuff that can be on the Left side of an assignment
r-values are stuff that can be on the Right side of an assignment

c++ is the greatest language of all time

Yes, but why does it matter? It seems to generalized, especially in C++ where there was also p-values and x-values and g-values or whatever.

int (*ptr)[size] = (int(*)[size]) new int[size];

I am struggling to make sense of this sentence

What are the rules here?

C++ to C is lung cancer to lung
/thread

The type is getting cast from an array of ints to a pointer of the array.

Here's an anime gril reading a book, feel free to shoop whatever programming book you want.

cute

who cares for the roolz as long as it has that new pointer smell?

cute
sauce?

>why does it matter?
The distinction exists so that the standards can talk about them, when describing what's valid where.

>especially in C++ where there was also p-values and x-values and g-values or whatever
The reason C++ has so many XYZ-values is because they keep subtely changing the standard. Honestly I don't know the different between all of them.

Anyone here good with Python?

I want to sort a list of classes by it's date attribute and I can't figure it out

Watching a great YouTube video series on Scheme.
I have implemented an iterative map procedure in Scheme, which is a bit retarded since it reverses the list twice.

They're all hacks to support move semantics.

humblebundle.com/books/python-by-packt-book-bundle

c++11 or 17 idk im just studying for a test.

I do not understasnd how to read it.

int (*ptr)[size] = (int(*)[size]) new int[size];

My Brain Thinking{

1- int (*ptr) probably means int* ptr
2- but then what is size !
3- (int(*)[size)), only clue is, and its probably wrong, is that this is pointer arithmetic
}

>x-values
It's used for a half-baked ownership model.
>pr-values
More stringently just r-values.
>g-values
x-value or l-value

It matters because move semantics among other things. It's a good reason to hate sepples because it's a necessity for what they've built for themselves.

That was my impression as well. But Rust only has l-values and r-values and it has better move semantics than C++. I wonder why

Because of affine types and the restriction that moves must just be memcpys.

>and the restriction that moves must just be memcpys.
Ah. Doesn't C++ get its panties in a wad because objects are allowed to contain pointers to themselves? One of those things that requires you to implement the 4+ RAII constructors.

People always get frustrated trying to implement cyclical things in Rust, but the reason it's not so simple is pretty obvious: objects can't contain pointers to themselves or to their own fields. The reasoning is of course because an object can be moved out of its previous location at any time

How do I learn programming?

read a book

The main issue is that, because C++ doesn't have a way to stop destructors being called on moved objects (affine types do this in Rust), movable types must support a null object with a no-op destructor. Then you need move constructors/assignment operators to null the original variable when the object is moved.

What books? Pls help I am retarded

When parsing complex declarations, you start going right and reverse direction whenever you encounter parentheses - ptr is a pointer to an array of ints.

Also, about Rust: it's fine for movable objects to store references to things they own without deep moves as long as those things are behind a pointer. Rust has self-referential objects coming somewhere down the line.

what kind of programming do you want to do? real programming? or fake?

i have no idea, because c is clearly for plebs.
looks to me like an initialization for an array of int pointers though, if that may or may not help.

I want to design software for busineses and as a hobby

probably start with c# or java

to add, C and C++ are worth knowing but honestly aren't that useful if you don't have a CS degree to go along with it

>When parsing complex declarations, you start going right and reverse direction whenever you encounter parentheses
user, i am now in my neighbours bedroom, what now?

Thanks boys, time to actually do something with my life

>When parsing complex declarations, you start going right and reverse direction whenever you encounter parentheses

int (*ptr)[size] = (int(*)[size]) new int[size];

My Brain Thinking Now{

read right,
readed int,
saw parenthesis,
reversed
readed int went forward
saw parenthesis
reversed
saw int
(im gunna see parenthesis again, maybe i shud remove them with magic)
cast magic on parenthesis
Now I see
int *ptr [size] = (int(*)[size]) new int[size];
What I really see
int* ptr [size] = (int(*)[size]) new int[size];

Brain stops dead in its tracks because its never seen int* ptr [3] before (size defined as 3)

Here's a (NSFW) visualization of the space of images my hentai neural network has learned to generate: my.mixtape.moe/kiyaun.webm

Good shit user.

That's some avant porn
I'll see if I can fap to it and let you know

how const method in C++ preserve their const-ness?
for example this code
class Foo{
int a;
int * b;
public:
foo() : a{0}, b{new int} {}
~foo() {delete b;}
int a_get() const {return a;}
void set(int x) {a = x;}
int * b_get() const {return a;}
};

class Bar{
foo a;
public:
int a_get() const {return a.a_get();}
void set(int x) {a.set(x);}
int * b_get() const {return a.b_get();}
};

class baz{
Bar bar;
Foo foo;
public:
void a_single_function() const {
bar.set(10); //im pretty sure this is not allowed
foo.set(10); //im also pretty sure this is illegal
*(bar.b_get()) = 5; //is this allowed?
*(foo.b_get()) = 5; //is this also allowed?
}
};

dumb frogposter

I don't really write much cpp, but I'm as confused as this user. Why couldn't this be written as
int* ptr = (int*) new int[size];
or similar?

do computers masturbate to this

Other than what you learn in school, what should you be teaching yourself in your spare time? What concepts should you spend time on? Does this depend solely on what job you want to do? What if you don't know?

40MB of good porn

Another thing too, once you learn starting a second language, do you start with the basics, as a beginner would, and work your way back up or do you pick up exactly where you are with the first language?

Foo::b_get has an int* return type but you're trying to return an int.
None of your four statements will work. The last two won't work because your method b_get returns a const pointer (well, it should in your example, which won't compile unless you do), and you cant edit a pointer to const

It's meant to be challenging, its from a school site. Is that actually the same thing though? I will compile and let you know!!

Depends on what you want to learn and what you do at school
Start from the basics. Definitely definitely do not try and start from your progress with another language.

...

pick something that you want to learn how to do and then use google and/or youtube to figure out how to do it

syntax doesn't take long if you already know mainstream language, but you should make sure you know the basics of what functionality the language offers (or does not offer) that you might not be used to

That's the thing I have no clue what I'd like to do. Only things that come to mind is security, game development, and porn. I'm learning CPP now, but I'm thinking about picking up JavaScript at some point. But I really want to get the fundamentals down with CPP first, dive into structures, then start JS. Is this a good plan?

although c++ is a good choice for what you're after, it's got a learning curve. if you learn modern c++ you will more than be able to write javascript (you'll also quickly learn to hate it), find yourself a good c++ book; remember that it's not a glorified C or some gotta-go-fast Java

on that topic, is there anything like this for c++?
cdecl.org/

compiled and nope its not the same

not that i know of, you got an example to show that you need explained?

what is /dpt/'s preferred method of storing lots of data, without using a db like mysql?

I'm using json, but there's a lot of data (~1.2G) so I'm having to gzip it and store it to a file. I plan on adding in a DB and using the file as a cache pretty soon but for now I'm still testing and need a faster way of storing and accessing data.

huh, good to know.
Did it behave differently or fail on compilation

You could use something like MongoDB or CouchDB which take a lot less time to get going than one of the big relational databases. And you can keep using JSON

only error is :
subscript requires array or pointer type, on those lines

I already got one. It's a book that I used for school awhile back. The only problem is that it's an older edition that was published in 2006. So I do worry I'm not learning the more modern concepts of c++. What's wrong with JS exactly? Also generally speaking, how old should the books I use be? I'm thinking no older than 3 years from this point forward?

If "ptr" is a "int*", then "*ptr" is a "int"
So
(*ptr) is an int
then you're trying to do
(*ptr)[0],
e.g., applying a subscript to an int
That doesn't make sense. That is the same as doing
int x = 5;
x[0]
or worse
5[0]
Simply doesn't make sense

neuralnetworksanddeeplearning.com/

Your book must teach c++0x (c++11) standard and no earlier. green flags are concepts like r-value references, perfect forwarding, variadic templates, smart pointers etc... red-flags for a book is when they recommend you ever use the word 'new'.

js is a usable language, that has some very odd behaviour if you go looking for it. it's the language forced upon all webdevs, and let's just say there's a reason that they've bothered creating whole languages to transcompile to javascript just so that they dont have to write javascript

int (*ptr)[size]


This is a pointer to an array of integers. If the parenthesis were not there, it would be an array of pointers to integers.

new int[size]

This is allocating size ints and returning a pointer to the first one.

(int(*)[size])

This is a C-style cast taking that pointer to an int and turning it into a pointer to an array of ints.

I'm a hack fraud brainlet. I just wanted to get it off my chest.

Whats wrong with new, and what is better!

I hope that makes you feel better friendo

OK, so what about something like:
int** ptr = (int**) new int[size];

No idea about the syntax. But what's the deal with specifying the size on both sides? If it's a pointer to an array of ints, why does the size of what it's pointing to matter? Could one not just reassign ptr to an array of ints with a completely different size?

Yotsugi Ononoki from monogatari series.

ok so in modern c++, stuff like int* is called a raw-pointer.
raw pointers don't necessarily own a variable:
non-owning pointer:
int x = 5;
int* y = *x;
owning raw pointer (bad):
int* y = new int;
owning raw pointer is considered incredibly bad practise because you need to handle deallocation yourself, and you shouldn't allocate to the heap without a good reason because it's very slow

Does that mean it's worth learning and getting really good at, or just that it's a bitch to learn in general? As far as the c++ I'm gonna stick with this book since the school I went back too is still using it, just a newer edition. And is JS a language you learn on its own or do you learn it with all the other webdev langs?

Either way I'm moving somewhat fast through my intro book, much faster than I thought I would(not having a job helps of course). And I'm planning to put in even more time. So I really want to pack in as much knowledge as I can

honestly, js is not a language i would recommend to learn alongside c++. why not try a different flavoured language, such as a functional language or a more procedural language? obviously if you ever intend to webdev then learn js

to be honest with you my opinion on javascript is of little value because i have a personal bias against it, so take my recommendations with a grain of salt

like user said, if you "new", there has to be a "delete" within the same scope, or you get a memory leak. instead of explicitly typing new and delete, you should just use smart pointers, i.e:
int numbers[] { std::make_unique(10) }
is the same as
int numbers[] = new int[10];
except the unique_ptr template wraps the new/delete call for you so you never have to explicitly write them

How to easily read a templated STL documentation declaration?
for example this is the first declaration of std::get(std::tuple)
template< std::size_t I, class... Types >
typename std::tuple_element::type&
get( tuple& t ) noexcept;

Compared to the documentation of vector below, that writing is so intimidating and confusing. Is there any rule of thumb or tips for reading any STL documentation?

vector:
template<
class T,
class Allocator = std::allocator
> class vector;

It's like I said, I'm still unsure about it. I figure this way, I'm a little more "versatile" skill wise, and plus I'd love to work for a site like Chaturbate. Other than porn I really don't see myself strictly being a webdev, but it's still early

what part is confusing you

> why does the size of what it's pointing to matter?
When you specify the size with "new", you're allocating (asking the OS for) exactly that many integers.

>Could one not just reassign ptr to an array of ints with a completely different size?
Yes you could. But if you try to write say, the 10th element when you only allocated for 3 elements, bad stuff will happen. Maybe a crash, maybe a security vulnerability.

>int** ptr = (int**) new int[size];
This is not a good idea because you allocated an array of integers, YET you're pretending that it's a pointer to a pointer to an integer. You're treating data as if its something it isn't.

Ensure you're comfortable with variadic templates and compile-time values (which is what "

If versatility is your wish, then definitely make space for another paradigm like functional. It may not seem like a solution given your use-cases, but it will improve your quality as a programmer

Sorry I should have been more specific. What's the significance of the first 2 size atoms in:
int (*ptr)[size] = (int(*)[size]) new int[size];
vs just defining ptr as a int**, since that will refer to the first element of the allocated array just the same and there's no restriction against arbitrary reassignment of ptr to any other size array or memory address anyway

+1, learning Erlang made a massive difference in my understanding of both functional and imperative programming

>Know basics of C#
>Have no idea how to really test my knowledge
>Friend tells me to get Unity and try to make a shitty game demo
Is this a good idea?

Please dont torture yourself with gayme development user

>be retard
>can't program
>try learning haskell
>tfw I can get on it
>tfw learning
holy shit, was this always so easy?
why do other languages look like soup letter vomit?
I'M LEARNING ANONS

LOOK!!!

That's wrong. More accurately, this will works because int and * are actually the same size in memory, but it's just disgusting to read.

You're basically casting an array of integer into an array of pointers to integers. Fortunately integers and pointers to integers are the same size, so it will work. It's bullshit and I would give you an C- for that unless you have a very good reason for it.

Not the perfect test of knowledge. But as long as you actually write some useful scripts and don't spend all day in the dumb editor it COULD work. But really not the best way to spend your time

It's because you have autism, so the only language that makes sense is autistic.

>Unity
>Good for learning
Jesus fucking christ, no.
Do old advent of code challenges and eulers project

At most I'm doing that as a hobby, zero plans on it as a career
Any recommendations for seeing where I'm at?

Sorry friend I can't think of an exercise for you that isn't flawed similarly to the Unity idea. Check out pro/g/ramming challenges perhaps? pic related

I'm not pretending I know which one is better. This is definitely for my learning. Which are you suggesting is disgusting, the coded one or the int**

>(*ptr)[0],
size is 3 in the pic/code provided

>so the only language that makes sense is autistic.
he didn't say he was writing lisp

ptr >
) <
* <
( >
[size] >
{end} <
int <
{start} STOP

Even most of those easy ones are beyond me

did u find a different way to write it, i can compile and check

I'm double checking with this other user whether he thinks what was written in your example was BS or not