C++17

Tobias Müller via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 26 23:37:26 PST 2016


H. S. Teoh via Digitalmars-d <digitalmars-d at puremagic.com> wrote:
> This reminds me of the time when I wanted to make a "trivial" change to
> an old C++ project of mine. This was after I had become acquianted with
> D, and gotten used to built-in AA's. I noticed that C++14 finally
> officially had hashtables in the standard library (which is worth a rant
> in itself -- who has ever heard of a modern programming language that
> has no standard hashtable implementation until how many decades
> after?!), and was eager to finally try it out in my code.  It would be a
> simple task, I thought; simply plop in the new standard hashtable at a
> strategic point in the code to serve as a cache, and performance should
> improve. Right? Right??
> 
> Many hours later, I was tearing my hair out trying to resolve stupid
> issues like:
> 
> 1) You cannot instantitate a hashtable template without also specifying
>    a hash function. WAT?! No built-in hash function??! Well, yes there
>    is, except...

Wrong.
There's std::hash<T>. It's a struct, not a function though.

> 2) The standard hashtables have no standard hash function for structs.
>    You have to write your own.  WAT??!

Usually, you just specialize std::hash<T> for your type.

How often do you write custom structs/classes (it's the same in C++) to be
used _by value_ as key in a hash table anyway?

> 3) After you have a hash function, you have to create the hashtable with
>    an explicitly-attached hash function

Wrong.

> -- it's not a template parameter,

Wrong again. It is a template parameter and it defaults to std::hash<T>.

>    it has to be a function object passed to the hashtable's
>    ctor.

With default argument std::hash<T>().

>    Which means passing around the hash function everywhere all
>    over the code, wherever there's the need to create a hashtable.

Wrong.
You just specialize std::hash<T> and be done.

>    Arghh...

Yes.

> I'm pretty sure with a "little" more effort I could have made it work...
> but seriously, that was several *hours* compared to the 2 seconds it
> takes in D to add an AA to your code.
> 
> You people who think there's still enough reasons to stick to C++ crack
> me up.  Do you have any idea what on earth you're talking about?!

Well apparently you don't.

Tobi 





More information about the Digitalmars-d mailing list