Sunday, 21 April 2019

C++: When to Cast Away Const

There's a rule, a very good rule, a brilliant rule, almost a law to not cast away const in C++.  There are legitimate reasons for this, and greater minds than myself have invented and enforced said coding standard.

Unfortunately, this doesn't mean there are not cases where you can use this technique, lets take a look.

So, modern C++ casts should always be used.  These are static_cast, reinterpret_cast, the dreaded dynamic_cast and as we're going to look at const_cast.  Where would I use this?  Well, in concert with good parameter conventions, we always want to communicate to the outside world what we'll use a parameter for...

class Foo
{
private:
    int* memory;


public:
    Foo(int* ourMemory)
        :
        memory(ourMemory)
    {
    }

};

We're assigning our memory to the class, but this constructor is communicating something back to the caller, it intimates that the pointer could perhaps, maybe, be changed as we pass it without the constant prefix.  A caller could then assume they must check whether the pointer has changed, become nullptr or whatever else.  That's assuming our API users are good people, which we all knows they are!

But, we don't change the pointer, we just take it into our member variable pointer.

We therefore have a dichotomy, the class may change the pointer along the way, but the actual constructor does not, this is a design decision, so assuming we communicate this back, I just want to make it clear the constructor does not change the pointer.

class Foo
{
private:
    int* memory;


public:
    Foo(const int* ourMemory)
        :
        memory(ourMemory)
    {
    }

};

Unfortunately you now get a compile error that "int*" can't be cast to "const int*".  So we need to cast-away the const in our constructor, like this:

class Foo
{
private:
    int* memory;


public:
    Foo(const int* ourMemory)
        :
        memory(const_cast<int*>(ourMemory))
    {
    }

};

This is, pretty much, the only use case I can find in my own coding standards for casting away const.

Monday, 25 March 2019

C++ Variadic Template Compiler Optimizations

In my prior post we briefly talked about how the compiler unrolls a variadic template and we needed to provide the end-point function to stop that iteration, lets take a closer look at how the compiler does this unrolling.

The impact this has on your debug build is critical, and I demonstrate that you can see the unrolling of numeric functions (such as summation) but then in release the compiler is smart enough to optimize that all away and use a constant.  Understanding when this is happening is a crucial lesson, as we then see the action of a repetitive string output.


If you found these two little videos of any use, please life & subscribe on YouTube to let me know!

And as always the comments below are available.

Sunday, 24 March 2019

C++ Variadic Template Parameter Packing Example

Today in the brief few mintues I hate, I wanted to welcome all the new readers the blog is getting, there are nearly 5000 of you folks passing through these pages each month!


And now the code video....

Today we go over a usage example for variadic templates, demonstrating the new C++17 parameter packing, how to call functions with the parameter pack, how to mix parameter types with the template and control the compilers iterative unrolling of your code.

Find the source code here:

Find Fedor Pikus's new book here:


Please Note: Filmed in one take, during the single half-hour I have spare, so don't complain about the low production values, cus the code does at least work!

Friday, 22 March 2019

P4

I don't like P4


Its not the best blog post after a fortnights silence, but it's all I have to say really.

Thursday, 7 March 2019

Mistakes Were Made

I had a lovely post for this evening, until a coworker pointed out it was utterly wrong... DOH.

Tuesday, 5 March 2019

C++ Factory Create Shared Pointer - Private Constructors

Back in 2016, I posted this little ditty about hiding the constructors but still enabling the use of "make_shared", this worked through a friendship between your class and the ref counter, but was sadly windows only.


At the time I promised the follow up for the same code for GNU, well I forgot, so here's the secret sauce, make your class as per that post, but friend it thus:

friend class __gnu_cxx::new_allocator<Foo>;

This only came up as I showed a colleague today and realized, I never got around to posting the other item!

Sunday, 3 March 2019

Home Network Upgrade - Part 1

This is going to have to be a multi-part series, firstly as it's a complex task we've got on, but also time constraints from both my side and also from suppliers delivering me kit means it's already spread itself over three weeks... and I can only imagine it's going to take a lot longer yet.

What am I up to?  Well, we're all settled in the new house and it's time to sort out the networking... Now I only have wireless at the moment, you may have seen my prior posts about the lost router?  Well, I found it... And I was wrong, it wasn't lost at the in-laws, it was lost here.... After searching the garage again, with the wife for that fourth time she finally believed me that it was lost (not just that I was being a stupid man) and she helped look elsewhere... We still didn't find it, however the next day at work my phone rang and on the other end was a joyous voice "I found it, the router, I found it"...

Yes, it was in a box in the garden!

Not open to the elements, but not fabulous either, anyway, router found I stuck it in place.  The problem now, I had two wireless networks the router provided one and the BT Homehub one... Easy, turn the BT Wifi off.... Which I did.

Except, that doesn't turn off the BT Wifi with Fon thing, the free access point for any BT customer to use.  And, I saw folks using my Wifi*

So, I hit the old interwebs and looked up a decent ADSL/VDSL modem replacement, the one which came up, from a video by Cameron Grey actually.  I ordered a Netgear DM200.


It looks quite nice, this is just an ADSL jack in and RJ45 jack out, it is NOT a wifi router.  But, this is an utter and total pig to set up folks, really a total and utter pain in the bum.... I tried three weekends in a row in six different sessions each of over an hour before it finally sorted itself out.

For those interested, flash the firmware to v1.61.  Give it power but no network connections plugged in and hold the factory reset button on the back for 30 seconds, then kill the power to it... Plug in the ADSL feed and connect it with a single ethernet cable directly to your laptop or PC.... Give it power and wait... and wait... and wait... until all three lights go green... If they don't after like fifteen minutes (seriously wait that long), only then remove the connections, but leave the power on factory reset again and power off, connect and power on etc until you get three green lights.

Then on your PC browser open to "http://routerlogin.net" and get in there, and just run the Genie wizard, if ANY thing else comes up, factory reset!  Let it detect BT, enter the username as bthomehub@btbroadband.com no password, nothing else and wait literally tens of minutes, it will get somewhere.  And when it's asking you to register on the netgear site pacify it with the "next" button until it is totally and utterly and completely done.  Only then open another tab and try a website.

When I finally got a website I then removed this hunk of trash.

Finally, no BT Wifi with FON sucking on my juices.  I set the DM200 into modem only mode, wired it to my Router and set up PPPoE with the username and password for BT again... Anyone wondering bthomehub@btbroadband.com and BT, with VLAN set to 101.

And voila, I was back to where I was with my virgin router at the old address, I now have my nice router working nicely with a nice modem on this BT line... And it's about 5mbps quicker on my speed tests, the DNS resolutions (set to Google and CloudFire are around 30ms quicker too).  And I have more control over everything, I am so much happier.

But I'm not altogether happy, because the next phase of my work has been massively massively delayed, by like a year... Like this was planned for the old house... Fiber Optic.

Yeah, remember this box?



Hang on, lets be clear... THIS BOX...


Yes, they are fiber optic network cards... You might ask "Xel why have you so many?"... And I would have to blush and admit, I thought I was buying two as a test, instead I bought TWO JOB LOTS OF 10 CARDS!  So  have this huge box full of 20 fiber optic network cards.

The plan therefore is to put together a small form factor PC by the router and fit a fiber connection to it.   Carry that fiber line up into the loft and up there have a fiber optic router.  That's the ultimate aim.  But I want to do that test first, machine to machine... So, I fitted a card to my workstation and another to my my Xeon home server... All fine, drivers and everything fine, and I set about ordering a pair of short 2 meter cables...

And I wait...

And wait....

And a month later finally....

Oh yeah baby... Except, hang on...


I ordered a pair of cables didn't I... Didn't I... Yes, yes I did... How many are in that baggy?... Just one... OH NO JUST ONE!!!?!?!??!?!!  So, this is where I leave this project for now, it's been a long time coming, but I hope to get the optics test done, then we've got two runs of ether cat 5 or fiber to run from the router feed in down stairs to the loft and then to my office and to the garage.

As owning a garage, this is where I now plan to have the server rack, and running fiber to it may be my optimal solution, it may even be possible to get cat 5 into the garage easier than to the loft, so all the fiber might go in the garage, this is literally a plan in the process of being figured out as I find out the structure and layout of the house.

Stick around for more.



* I actually spoke to a neighbour about this, he said "Oh that'll be my lad, he connects to the as many BT Wifi connections for free, we don't pay for the internet that way".... Nice, thanks...