I was just over specializing std::hash
for a user-defined type using:
template<>
struct hash<...> {...};
When VC10 greeted me with the warning:
warning C4099: 'std::hash<_Kty>': type name first seen using 'class' now seen using 'struct'
and I found out that its standard library declares std::hash
as class
, whereas the standard (or the latest free draft I have) declares it as struct
.
Well, of course I know that a struct isn't any different from a class (except for the different default access and inheritance types). But my questions are:
struct
s for class
s in any standard library components (as long as the required access types for members stay consistent, of course)?The entities of variable, function, struct, and class can have templates, which involve declaration and definition. Creating a template also involves specialization, which is when a generic type takes an actual type. The declaration and the definition of a template must both be in one translation unit.
The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.
Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.
Explicit (full) specializationAllows customizing the template code for a given set of template arguments.
First off, here is the answer to 2. taken from 14.5.1 [temp.class] paragraph 4:
In a redeclaration, partial specialization, explicit specialization or explicit instantiation of a class template, the class-key shall agree in kind with the original class template declaration.
However, struct
and class
are referring to the same class-key according to 7.1.6.3 [dcl.type.elab] paragraph 3 last sentence:
The class-key or enum keyword present in the elaborated-type-specifier shall agree in kind with the declaration to which the name in the elaborated-type-specifier refers. [...] Thus, in any elaborated-type-specifier, the enum keyword shall be used to refer to an enumeration, the union class-key shall be used to refer to a union, and either the class or struct class-key shall be used to refer to a class declared using the class or struct class-key.
Trying g++, clang, and EDG all agree that it is possible to specialize a template declared as struct
as a class
. However, clang warns about having changed from struct
to class
or vice versa. Based on this, the standard library is free to choose whatever keyword it sees fit for the definition. Obviously, if the compiler rejects the code as a result something is seriously broken but I'd think it is the compiler rather than the library which is at error in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With