Topic: Why can't you specialize member template classes?


Author: kristov@arcor.de (Christoph Schulz)
Date: Thu, 15 Jul 2004 16:13:23 +0000 (UTC)
Raw View
Hello!

Ivan Godard wrote:

> Consider:
>
> template<typename T> struct  A {
>     template<int n> struct B;
>     B<sizeof(T)> b;
>     };
>
> template<typename T>
> template<int n>
> struct A<T>::B { };
>
> template<typename T>
> template<>
> struct A<T>::B<1> {};
>
> int main() {
>     return 0;
>     }
>
> [...]
>
> Why is this natural idiom illegal? I don't see how it is ambiguous to
> parse.

First, your code is disallowed due to paragraph 14.7.3/18 of the
standard, which says:

> In an explicit specialization declaration for a member of a class
> template or a member template that appears in namespace scope, the
> member template and some of its enclosing class templates may remain
> unspecialized, except that the declaration shall not explicitly
> specialize a class member template if its enclosing class templates
> are not explicitly specialized as well.

Second, the reason why it is disallowed is (I think) the fact that
A<T>::B does not have to be the same type for any T. It does even not
have to remain a type. E.g., someone could say:

template <> struct A<int> {};

without any B define inside. Then A<int>::B<1> would not be legal.

Because of this, it is required that if inner members are specialized,
then so the enclosing ones.

Regards,
  Christoph Schulz

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: sebormartin@netscape.net (Martin Sebor)
Date: Mon, 28 Jun 2004 15:33:33 CST
Raw View
..
> Thank you. Now how does one make it a proposal (or find out if it already
> is)?

You might want to start by searching the C++ Standard Core Language
Issues List
here: http://www.open-std.org/jtc1/sc22/wg21/.

If you don't find an issue for this on any of the lists you can file a
Defect Report against the standard and propose a change allowing the
feature. To learn more about Defect Reports, read the FAQ for this
newsgroup:
http://www.jamesd.demon.co.uk/csc/faq.html#B12

Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: igodardA@TpacbellDO.Tnet ("Ivan Godard")
Date: Wed, 23 Jun 2004 17:38:06 +0000 (UTC)
Raw View
Thank you. Now how does one make it a proposal (or find out if it already
is)? Any lawyers out there? Or someone who knows what fatal ambiguity sich a
proposal would introduce?

Ivan

"edA-qa mort-ora-y" <usenet@disemia.com> wrote in message
news:40d52fef$0$26367$9b4e6d93@newsread4.arcor-online.net...
> Ivan Godard wrote:
> > Why is this natural idiom illegal? I don't see how it is ambiguous to
parse.
>
> I support Ivan on this, I don't understand why this structure should be
> illegal.  I have several pieces of code that could also benefit from
> such a structure (though for now I'm using namespace polluting
> workaroungs ;)
>
> I don't thing there is any ambiguity with it, as the GCC compiler
> appears to recognize exactly what you want, but then just says it isn't
> allowed.
>
> --
> edA-qa mort-ora-y (Producer)
> Trostlos Records <http://trostlos.org/>
>
> "What suffering would man know if not for his own?"
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: igodardA@TpacbellDO.Tnet ("Ivan Godard")
Date: Sat, 19 Jun 2004 23:31:37 +0000 (UTC)
Raw View
Consider:

template<typename T> struct  A {
    template<int n> struct B;
    B<sizeof(T)> b;
    };

template<typename T>
template<int n>
struct A<T>::B { };

template<typename T>
template<>
struct A<T>::B<1> {};

int main() {
    return 0;
    }

This gets you (gcc 3.4):
~/ootbc/common/test/src$ g++ foo.cc
foo.cc:11: error: invalid explicit specialization before '>' token
foo.cc:11: error: enclosing class templates are not explicitly specialized
foo.cc:12: error: template parameters not used in partial specialization:
foo.cc:12: error:         `T'

The intent is that regardless of the type T, the type A<T>::B should be
specialized when the size of T is 1. The above idiom keeps B nicely local
inside A, but is forbidden. All workarounds either pollute the namespace
outside A in some way, or require that member b be a reference or pointer
with concommitant overhead.

Why is this natural idiom illegal? I don't see how it is ambiguous to parse.

Ivan

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: usenet@disemia.com (edA-qa mort-ora-y)
Date: Mon, 21 Jun 2004 16:58:43 +0000 (UTC)
Raw View
Ivan Godard wrote:
> Why is this natural idiom illegal? I don't see how it is ambiguous to parse.

I support Ivan on this, I don't understand why this structure should be
illegal.  I have several pieces of code that could also benefit from
such a structure (though for now I'm using namespace polluting
workaroungs ;)

I don't thing there is any ambiguity with it, as the GCC compiler
appears to recognize exactly what you want, but then just says it isn't
allowed.

--
edA-qa mort-ora-y (Producer)
Trostlos Records <http://trostlos.org/>

"What suffering would man know if not for his own?"

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]