Pokitto 1st Magazine Entries

magazine

#21

This is also kind of my point.

Using templates from a library and writing your own templates are two very different things.

Almost anyone of any skill can make use of templates from the standard library (e.g. std::swap, std::min, std::max, std::array, std::vector, std::move…),
but writing templates is something you can’t do until you can at least write functions and/or classes.

If you’re not concerned with all the ins and outs of templates then writing basic templates doesn’t take too much explaining,
but if you’re worried about things like what happens here:

int func(int)
{
	return 0;
}

template<typename Type>
int func(Type)
{
	return 1;
}

const int value = func(5);

Then it takes more explaining.

Most of the most common and most useful template classes and functions are already available as part of the standard library.

<algorithm> is a header full of template functions designed for doing operations on iterators.

Some examples:

int data1[] = { 0, 1, 2, 3, 4, 5 };
int data2[6];

// Cheap and easy array copying
std::copy(std::begin(data1), std::end(data1), std::begin(data2));

// Add 1 to every element in data1
std::transform(std::begin(data1), std::end(data1), std::begin(data1), [](int value){ return (value + 1); });

// Reverse the contents of data2
std::reverse(std::begin(data2), std::end(data2));

// Create a pseudo-random number generator
std::mersenne_twiser_engine randomGenerator { /*Generate a seed somehow*/ };

// Prepare an array
unsigned int randomInts[32];

// Fill randomInts with random numbers
std::generate(std::begin(randomInts), std::end(randomInts), randomGenerator);

There’s a few useful functions that didn’t make it into the standard library until C++17.
For example std::size to give you the size of any array or container,
and std::clamp which is a sort of combined std::min and std::max.

However, those aren’t as trivial to write as you might expect.
std::clamp has to use const references (the same as std::min and std::max) for several reasons:

  • Using pass-by-value could be inefficient for certain types, or even impossible for types that have disabled their copy constructors. Using a const reference allows a reference to be made to an object that can’t be copied.
  • Using a non-const reference would prevent the function from accepting const arguments, including literal constants (e.g. integer literals), and in other cases could cause unexpected behaviour if someone tried to modify the result (e.g. ++std::min(a, b); would modify either a or b).

And std::size makes use of SFINAE and trailing return types.

At any rate, simply knowing how to write templates isn’t necessarily going to be an immediate benefit.
Templates are useful in the case that you want to write a class or function that needs to be able to operate on more than one type.

For example, if you were implementing a priority queue (since there isn’t one in the standard library) or some kind of tree and you wanted the nodes to be able to hold any type.
Or if you wanted to implement Points and Vectors without constraining them to a specific numeric type, so you could have Point<int>, Point<float>, Vector<int> and Vector<float>.

None of that is the sort of thing I’d expect to see a beginner doing.


#22

@Pharap why don’t you start with the basic as if you talk to a beginner? Any skilled programmer won’t really need to read that anyway right?

Also, you are almost doing the article right here while you try to explain why you shouldn’t do an article about it, which I find funny lol

But again, I think we should focus on the basics first. Another reason why we don’t have that much people here, I think, is because we need step by step tutorials explaining how to get started. How to first display something in the Pokitto screen, how to control, add inputs using the Pokitto buttons etc… This is to get people hooked and wanting to actually learn and create more elaborated things later.
If I am a beginner and you start talking about templates, I will just turn around and walk away. But if you first let me do something cool, and then when I have a certain understanding, let me know there is something called templates to that can be usefull if I want to take it to the next level, that’s something else. You have to first know what’s a template is trying to help you with before starting to learn what they are and why should we use them. I may be wrong, but it’s just how I see it.


#23

Here, by free culture I just basically mean the combination of free/open-source SW and free assets. But I’d like to include a bit of history as well, because I think it’s important to know why things are the way they are, sans any propaganda of course :slight_smile: Basically like the article I’ve written for the Arduboy magazine, but with a bit of historical context. Not sure if this should be two articles (free SW/culture + licensing) or one.


#24

This is why I’m making a point of asking what people actually want to know.

For someone who is only just starting to program, learning how to use some of the template functions available in the standard library is probably as far as they need to go.

Learning how to write templates requires understanding functions and classes as a minimum.

Learning how to write good templates also requires an understanding of things like references and how template parameter substitution works.

Hence why I say:

You’d be surprised how many skilled/experienced programmers don’t actually know how to write templates.

I’m not saying I shouldn’t do an article about it, I’m saying it’s impossible to write an article about it without pinning down which aspect of templates people want the article to be about (in ascending order of difficulty (to understand, not to write)):

  • A beginner’s guide about how to use templates
  • A beginner’s guide about how to use the templates available in the standard library (specifically)
  • A journeyman’s guide about how to write basic templates
  • An in depth guide about how template deduction actually works

By asking for the basics, you’re effectively asking for something different to what @Hanski is asking for, which is my point.

I don’t want to write a long article about writing templates if people are going to turn around and say “that’s too complicated”.
I don’t want to write a long article about using templates if people are going to turn around and say “that’s too easy”.

I need to know what people actually want before I spend time writing.


To me that should still come under ‘licencing’.

I.e. The Xerox printer story?

I think just saying “legally you need permission to modify things and these licences give you that permission, whereas other licences don’t” is enough to get the idea of open source licencing across.

Probably two separate articles.
One for the practical aspects, the other for the history (and anything Stallman related).


#25

I would be interested to hear about the history.


#26

The Xerox printer story:

https://www.oreilly.com/openbook/freedom/ch01.html

I’m sure Stallman had a version somewhere on his site, but I can’t find it.
While I admire his site’s 90s-esque minimalism, it’s a pain in the neck to find what you’re looking for.


#27

Gutenberg has the book that @Pharap linked in several formats:

Another book that would be my source is Free Culture by Lessig (the founder of Creative Commons), very worth reading (he’s generally considered much less crazy than Stallman):

http://www.free-culture.cc/freeculture.pdf

There’s a lot of very interesting history of technology and copyright in that book, e.g. the fight of FM vs AM radio (starts at last paragraph of page 3 if you want to read it).

There are also other nice books, but they’re not free:

  • Linus Torvalds & David Diamond: Just for Fun – The story of Linux and the creator’s bio. Awesome read.
  • Glyn Moody - Rebel Code – Book generally about various FOSS projects and people, e.g. Perl and Larry Wall.

I also have Rufus Pollock: The Open Revolution and Peter Wayner: Free For All on my to-read list, but haven’t gotten to them yet.


#28

I have no idea what to write about or how to write it. Please don’t be offended if I come up with nothing at all :frowning:


#29

We demand history & strategy guide to Noggin!


#30

That would be amazing!


#31

Of course, we are not offended :slight_smile:

But you have such many wonderful projects! I know, it is not always easy to sit down and write something. How would you explain some of your HW project to a person who do not have a clue but would like to learn? Would it be easier if we do an interview?


#32

That’s why I talked about some kind of questionnaire in another post. That would help getting the juice out LoL :laughing:


#33

I have once written an article about licensing basics for the Arduboy magazine, which @Pharap gave me a lot of feedback on and we put a lot of time into discussing and adjusting it – not sure if it actually got posted eventually – but I think I could just edit it a little (s/Arduboy/Pokitto/g) and maybe submit as the first article… no need to write it anew, right? Any objections?


#34

Second submission is in! This is getting very exciting :smiley:


#35

So quickly? I have only barely started
:slight_smile:


#36

I want to write up an article too on how I use platformIO with fedora linux :slight_smile:

Edit: Well bummer, it seems to not be installing very easily… (I’ve been using other methods as of late :wink: )
Guess I’ll try and think of a different article I could write :thinking:


#37

I have now the example program and the preliminary article text ready :stuck_out_tongue_winking_eye:

image

I still need to put pictures, links and code clips there, and refine the article


#38

Is it ok to use a kind of “info boxes” in the article? I mean that kind of info which I do not want to put in the main text but can be useful for people who want deeper understanding of the subject?


#39

Of course!


#40

Another amazing submission is in and beginning the review process! This is extremely exciting to read through the submission. This is going to be so great!