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

template class vs struct keyword for classname

222 views
Skip to first unread message

Venkat

unread,
Jul 18, 2012, 6:07:01 PM7/18/12
to
{ Reformatted; please limit your lines to 70 characters -mod }

Hello All,

Though it seems a simple question, I couldn't find any authoritative
explanation, so asking. Here is an example:

template <typename T> class Foo;
template <> struct Foo<uint16_t> { };
template <> struct Foo<uint32_t> { };

gnu compiler accepted above implementation, and it's working. clang
raises an error about class/struct intermix.

I looked briefly through C++std 03 draft, and actually, syntax rules
shows only 'class' keyword in template declarations. But Comeau FAQ
uses struct, but did not come across intermix.

>From std 03 A.12 for Templates:
type-parameter:
class identifieropt
classidentifieropt = type-id
typename identifieropt
typenameidentifieropt = type-id template < template-parameter-list >
class identifieropt
template < template-parameter-list > class identifieropt = id-expression

What's the right answer?

Thanks
Venkat


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Anthony Williams

unread,
Jul 20, 2012, 4:14:47 AM7/20/12
to
On Wednesday, 18 July 2012 23:07:01 UTC+1, Venkat wrote:
> { Reformatted; please limit your lines to 70 characters -mod }
>
> Hello All,
>
> Though it seems a simple question, I couldn&#39;t find any authoritative
> explanation, so asking. Here is an example:
>
> template &lt;typename T&gt; class Foo;
> template &lt;&gt; struct Foo&lt;uint16_t&gt; { };
> template &lt;&gt; struct Foo&lt;uint32_t&gt; { };
>
> gnu compiler accepted above implementation, and it&#39;s working. clang
> raises an error about class/struct intermix.
>
> What&#39;s the right answer?

struct and class can be mixed, as you've shown. I wrote a blog post about this:

http://www.justsoftwaresolutions.co.uk/cplusplus/struct_and_class.html

Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

Daniel Krügler

unread,
Jul 20, 2012, 12:25:12 PM7/20/12
to
On 2012-07-19 00:07, Venkat wrote:
> Though it seems a simple question, I couldn't find any authoritative
> explanation, so asking. Here is an example:
>
> template <typename T> class Foo;
> template <> struct Foo<uint16_t> { };
> template <> struct Foo<uint32_t> { };
>
> gnu compiler accepted above implementation, and it's working. clang
> raises an error about class/struct intermix.

What do you refer to as "error" (I assume that uint16_t and uint32_t are
defined)?

> I looked briefly through C++std 03 draft, and actually, syntax rules
> shows only 'class' keyword in template declarations. But Comeau FAQ
> uses struct, but did not come across intermix.
>
>>From std 03 A.12 for Templates:
> type-parameter:
> class identifieropt
> classidentifieropt = type-id
> typename identifieropt
> typenameidentifieropt = type-id template < template-parameter-list >
> class identifieropt
> template < template-parameter-list > class identifieropt = id-expression
>
> What's the right answer?

The above quoted part of the standard does not answer your question, it
just refers to the grammar of the template prefix that occurs *before*
the actual class-key of a class template (Simply expressed: everything
between the initial "template<" and the closing ">").

I think the relevant part is specified by [temp.class] p4:

"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 (7.1.6.3)."

and via delegation by [dcl.type.elab] p3:

"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. This rule also applies to the form of
elaborated-type-specifier that declares a class-name or friend class
since it can be construed as referring to the definition of the class.
Thus, in any elaborated-type-specifier, the enum keyword shall be used
to refer to an enumeration (7.2), the union class-key shall be used to
refer to a union (Clause 9), and either the class or struct class-key
shall be used to refer to a class (Clause 9) declared using the class or
struct class-key."

While I find the wording forms used ("shall agree in kind") a bit
ambiguous I interpret them to intend that both "class" and "struct" used
as class-keys can be used interchangeably for redeclarations of
non-union class types and non-union class templates. Especially the last
sentence of [dcl.type.elab] p3 makes that clear ("either the class or
struct class-key shall be used to refer to a class").

HTH & Greetings from Bremen,

Daniel Kr�gler

cart...@gmail.com

unread,
Jul 20, 2012, 12:25:41 PM7/20/12
to
On Wednesday, July 18, 2012 5:07:01 PM UTC-5, Venkat wrote:
> [Can the class-key in an explicit specialization differ
> from the class-key used in the declaration of the primary template?]

ISO/IEC 14882:2011(E) 14.5.1/4 states: 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 (7.1.6.3).


Yordan Naydenov

unread,
Jul 21, 2012, 10:37:29 AM7/21/12
to
"Anthony Williams" wrote:

> On Wednesday, 18 July 2012 23:07:01 UTC+1, Venkat wrote:
>> Hello All,
>>
>> Though it seems a simple question, I couldn't find any
>> authoritative
>> explanation, so asking.

You may find it in section 1.2 A Keyword Distinction of Stanley
Lippman's Inside the C++ Object Model. Moreover, the primary focus is
not on the technicalities, but rather on both historical and
philosophical perspectives. Oddly enough, these, combined with the
first-(implementor's)-hand account, gives a good sense of when and
where to use or not to use the struct keyword in the context of C++.
Another great asset is the enjoyable and witty narrative. Have a nice
reading!

> struct and class can be mixed, as you've shown. I wrote a blog post
> about this:
> http://www.justsoftwaresolutions.co.uk/cplusplus/struct_and_class.html
>
> Anthony

It was a pleasure to read your article on that (ever emerging) topic.
Concise yet (technically) complete, clear and readable, well
structured in form and content. I hope the book of yours holds such
qualities as well.
Nevertheless, let me recommend two additions for the sake of
completeness, namely the nested class member and the (explicit)
template specialization/instantiation issues.
First of all, it would be nice if the nested class member is treated
and exemplified on equal terms with the class data member and class
member function/operator.
Secondly, the template section could be extended to encompass not all
the possible combinations of 'ordinary'/template class/struct
containing member templates and nontemplate members (for both nested
classes and member functions), which would be highly redundant, but to
depict the situation concerning the downstream refinement of a
template entity.
The sequence in question is: (class/struct template declaration ->)
class/struct template definition (primary template) -> partial/full
specializations -> explicit instantiation of a template class/struct
(of primary templates/partial specializations). (Please, refer to my
post-reply to Seungbeom Kim's 'What is template specialization?' to
this newsgroup (January 12th) for details.)

Are the class/struct keywords interchangeable in this sequence from
the viewpoint of the Standard's prescription not of the compilers'
tolerance? The answer may seem obvious...

Regards,
Yordan



--
0 new messages