Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

My C++ blog

65 views
Skip to first unread message

mvor...@gmail.com

unread,
Jan 29, 2019, 8:58:19 PM1/29/19
to
hi there!
so I started my own blog on C++ :) hope you guys will follow it and find the information useful. I've been coding professionally in C++ for 15 years and decided it's time to share my experience. I just created this blog yesterday and I'm starting slow with easy topics. hope to have time to post about various and more advanced topics about C++ and OO.

Enjoy!!!

https://blog.vorbrodt.me
====================

Alf P. Steinbach

unread,
Jan 29, 2019, 10:23:53 PM1/29/19
to
You should turn on commentary on your blog, otherwise incorrect stuff
will not be corrected, improvements and alternatives will not be
available, and people might decide not to read just because any serious
blog has commentary.

In my experience with Wordpress (which you use for the blog) it does let
through the odd spammer's comment, but not so much that it's a problem.

Re your second posting about converting C++ `enum` values to strings,
that's a complex problem because it depends on what the `enum` is used
for. E.g. is it used as named bits of a bitset? That's common, and
requires different code for conversion to string than simple single values.

For example, your function

const char* MyEnumToString(MyEnum e)
{
using MapType = std::map<MyEnum, const char*>;
static MapType kMap =
{
{ MyEnum::V1, "V1" },
{ MyEnum::V2, "V2" },
{ MyEnum::V3, "V3" },
};
assert(kMap.size() == (int)MyEnum::SIZE);
return kMap[e];
}

can /silently/ expand the map with a new entry, and then in a subsequent
call, from somewhere else half a galactic diameter away, trigger the
`assert`.

You could avoid that simply by adding `const` and using `at` or `find`
instead of `operator[]`.

For the case of single values that can't be combined, and where all
possible values are named, your code would work but is not reusable.

Reusable `enum` stringification typically relies on the trick of
redefining a macro, like this:

--------------------------------------------------------------------------
#include <algorithm> // std::find
#include <iterator> // std::(begin, end)
#include <string> // std::string

//----------------------------------------- Reusable machinery:

#define IMPLEMENT_ENUM_NAME_FUNC( Enum ) \
static auto name( const Enum id ) \
-> std::string \
{ \
static struct{ Enum id; const char* name; } names[] = \
{ ENUM_ITEM_LIST }; \
\
using namespace std; \
const auto p_pair = find_if( \
begin( names ), end( names ), \
[=]( const auto& pair ) { return pair.id == id; } \
); \
\
return (p_pair == end( names ) \
? #Enum "(" + to_string( id ) + ")" \
: string( p_pair->name) \
); \
}

//----------------------------------------- Usage example:

#define PHILOSOPHER_ENUM_ITEMS \
ENUM_ITEM( kant ) \
ENUM_ITEM( descartes ) \
ENUM_ITEM( socrates ) \

#undef ENUM_ITEM
#define ENUM_ITEM( name ) name,
#undef ENUM_ITEM_LIST
#define ENUM_ITEM_LIST PHILOSOPHER_ENUM_ITEMS
struct Philosophers
{
enum Enum{ PHILOSOPHER_ENUM_ITEMS n_values };
};

#undef ENUM_ITEM
#define ENUM_ITEM( name ) {Philosophers::name, #name},
IMPLEMENT_ENUM_NAME_FUNC( Philosophers::Enum )

#include <iostream>
auto main()
-> int
{
using namespace std;
for( int i = 0; i < Philosophers::n_values; ++i )
{
cout << name( Philosophers::Enum( i ) ) << endl;
}
cout << endl;
cout << name( Philosophers::Enum( 1234 ) ) << endl; // A not
named value.
}
--------------------------------------------------------------------------


Cheers!,

- Alf

Richard

unread,
Jan 30, 2019, 11:59:47 AM1/30/19
to
[Please do not mail me a copy of your followup]

"Alf P. Steinbach" <alf.p.stein...@gmail.com> spake the secret code
<q2r5bv$i3j$1...@dont-email.me> thusly:

>In my experience with Wordpress (which you use for the blog) it does let
>through the odd spammer's comment, but not so much that it's a problem.

I've had very good results rejecting spam through wordpress.com, so it
is possible to configure wordpress to be very good at rejecting spam.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Alf P. Steinbach

unread,
Jan 30, 2019, 8:08:11 PM1/30/19
to
On 30.01.2019 17:59, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> "Alf P. Steinbach" <alf.p.stein...@gmail.com> spake the secret code
> <q2r5bv$i3j$1...@dont-email.me> thusly:
>
>> In my experience with Wordpress (which you use for the blog) it does let
>> through the odd spammer's comment, but not so much that it's a problem.
>
> I've had very good results rejecting spam through wordpress.com, so it
> is possible to configure wordpress to be very good at rejecting spam.

Oh. I didn't even know one could do that.

Re the code I posted it was ugly, just the first the worked,
unreasonably half-way between two reasonable solutions. For consecutive
enum values it would be simpler and more efficient to generate a
`switch`. And for the case of wanting two-way conversion or ability to
iterate over value/string pairs, the collection of value/string pairs
should be provided on its own and not just a local static in a function.

The OP has still not turned on commentary, but has posted summaries and
one full quote of commentary he's received on Reddit. That approach did
/not/ work out well. I mentioned, in the posting up-thread, that lack of
commentary function on the blog could lead to

> incorrect stuff will not be corrected, improvements and alternatives
> will not be available

and just that has happened. :(

In particular, the January 27 posting “shrink_to_fit that doesn’t suck”
still provides only solutions (to a non-existing problem) that do suck,
and the January 30 posting “What happens when we “new”” incorrectly and
misleadingly states that when one writes

T* t1 = new T;

the compiler allegedly translates that to (really, it does NOT force use
of the global allocation function)

T* t2 = (T*)::operator new(sizeof(T));
try
{
new (t2) T;
}
catch(...)
{
::operator delete(t2);
throw;
}

The incorrect belief in that correspondence was once the basis of an
infamous bug in Microsoft's MFC framework, where one could get a memory
leak that manifested only in debug builds.

So, it seems we have a new C++ blog that provides some misleading
dis-information, sort of like Herb Schildt, with commentary turned off. :(


Cheers!,

- Alf

mvor...@gmail.com

unread,
Jan 30, 2019, 9:01:01 PM1/30/19
to
the comments are open on the blog. please post corrections. if I put misleading information I want to learn and correct it.

mvor...@gmail.com

unread,
Jan 30, 2019, 9:02:03 PM1/30/19
to
0 new messages