Toon posts:

[C++] Explicit template specializations in class definitions

Pagina: 1
Acties:

Verwijderd

Topicstarter
Waarom zijn explicit template specializations niet, en partial template specializations wél binnen een class definitie toegestaan?

C++:
1
2
3
4
5
6
7
8
9
10
11
struct A
{
  template <typename T1, typename T2>
  struct B {};
 
  template <typename T1> // Ok: partial specialisation
  struct B<T1, void> {};
 
  template <> // Error: explicit specialization
  struct B<void, void> {};
};
Comeau zegt:
error: explicit specialization is not allowed in the current scope

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 23:05

.oisyn

Moderator Devschuur®

Demotivational Speaker

compileert prima hier (MSVC 7.1 beta)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Topicstarter
En de nieuwste G++ is 't met Comeau eens..

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 23:05

.oisyn

Moderator Devschuur®

Demotivational Speaker

en als je de specializatie buiten struct A brengt?

dit dus:

C++:
1
2
3
4
5
6
7
8
9
10
11
struct A
{
  template <typename T1, typename T2>
  struct B {};

  template <typename T1> // Ok: partial specialisation
  struct B<T1, void> {};
};

template<>
struct A::B<void, void> {};

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Topicstarter
Dat werkt prima, maar ik zoek nog altijd het antwoord op de vraag.

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 23:05

.oisyn

Moderator Devschuur®

Demotivational Speaker

toch wazig, waarom zou een partial specialization wel mogen terwijl een explicit specialization niet mag :?

Heb even vlug door de ANSI C++ documentatie gebladerd, maar niet zo snel iets gevonden waarom staat of en waarom het niet mag
(geeft de Comeau compiler geen errorcode die je weer op kunt zoeken in de bijbehorende documentatie ofzo?)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Topicstarter
.oisyn schreef op 30 oktober 2002 @ 00:55:
toch wazig, waarom zou een partial specialization wel mogen terwijl een explicit specialization niet mag :?
'Wazig' lijkt me een uitstekende beschrijving :).
Heb even vlug door de ANSI C++ documentatie gebladerd, maar niet zo snel iets gevonden waarom staat of en waarom het niet mag
Mijn ISO-IEC14882-1998.pdf bood ook al geen uitkomst nee..
(geeft de Comeau compiler geen errorcode die je weer op kunt zoeken in de bijbehorende documentatie ofzo?)
Nee, en G++ ook niet.

Verwijderd

Topicstarter
Hmm:
ISO-IEC 14882, 14.7.3 paragraaf 2:
An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member.
Hieruit kunnen we echter niet opmaken waarom het voor explicit specializations anders is dan voor partial specializations..

Verwijderd

Topicstarter
Er blijken nog meer vreemde verschillen tussen partial en explicit member template specializations te zijn (14.7.3 paragraaf 17):
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <typename T>
struct A
{
  template <typename U, typename V>
  struct B {};
};
 
// partial specialization: ok
template <typename T>
template <typename U>
struct A<T>::B<U, int> {};
 
// explicit specialization: error
template <typename T>
template <>
struct A<T>::B<int, int> {};

De workaround hiervoor (B een extra template parameter geven met een default waarde) is helaas minder pijnloos dan die van het vorige probleem.

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 23:05

.oisyn

Moderator Devschuur®

Demotivational Speaker

* schop *

hoopt op zinnige verklaring van MSalters

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 10-12-2025
Ja, en 14.5.4 /3 [temp.class.spec] "Each class template partial specialization is a distinct template" zegt ook veel. De explicit specialization is geen distinct template (maakt uit bij friend definitions).

Het zijn dus andere beesten.
Heb effe niet heel veel tijd, compleet verhaal komt nog wel

edit:
Deze quote is ook wel goed: 14.7.3/7 [Explicit specialization]
When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

[ Voor 0% gewijzigd door MSalters op 30-10-2002 15:59 . Reden: leuke quote ]

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein

Pagina: 1