On Wed, 10 Jun 2015 05:17:32 -0700 (PDT)
Öö Tiib <
oot...@hot.ee> wrote:
> On Tuesday, 9 June 2015 22:52:38 UTC+3, Chris Vine wrote:
[snip]
> > I have come across this outside VS when specializing std::hash for a
> > type which might be used as the key for an unordered container. The
> > standard does not specify whether the std::hash<T> class template
> > type is defined using the 'class' or 'struct' keyword. §20.8.12 of
> > the C++11 standard uses the 'struct' keyword when specifying the
> > specializations required by the standard, but only requires that
> > any given specialization must comprise a 'class template'.
>
> Actually it is because that 'class' and 'struct' class-keys are usable
> interchangeably. I do not see any benefit from it so it is not clear
> why it was made that way.
Indeed. The standard only requires a class template for std::hash,
which can be declared with either keyword. The only difference between
the keywords (as far as I am aware) is the default access to members and
default inheritance.
> > clang exhibits the annoying behaviour of emitting a bogus warning if
> > you provide a hash specialization using a different keyword from the
> > one which happens to be used by the C++ standard library in question
> > for std::hash<T>, which means that if you use clang with a recent
> > version of libstdc++ you get the warning when using the 'struct'
> > keyword instead of the 'class' keyword for a specialization. I am
> > glad to say that gcc does not do this.
>
> It seems 'std::hash' is a 'struct' in libstdc++ so consistent would
> be to specialize it also as 'struct' (not 'class')?
With the version of libstdc++ that comes with my current version of gcc
(5.1.0), that indeed seems to be right. Maybe I am misremembering and
the user code provoking this warning used the class keyword rather than
the struct keyword when specializing std::hash, but I notice that there
is some inconsistency within libstdc++ headers - some classes declare
std::hash as a friend using the class keyword. Maybe that was what was
triggering the warning when using clang. Anyway, I cannot reproduce it
now if the struct keyword is used.
> Still it is merely about a warning or lack of one. What is strange
> is the OP's linking error. What can be a reason to make even the
> ABI different for 'struct' and 'class' (or for respective pointers)?
Yes, clang still compiles code with mismatching keywords on
specialization so it is not from that point view a bug, but it is
annoying to have bogus warnings: I prefer warnings which are actually
worth warning about (and this particular one can be switched off with
-Wno-mismatched-tags compiler flag). On the other hand, the failure to
link correctly where there are mismatching keywords appears to be a
real bug on VS's part.
Chris