public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* template specializiation
@ 2005-07-07 13:42 Thomas Neumann
  2005-07-07 14:46 ` Eljay Love-Jensen
  0 siblings, 1 reply; 8+ messages in thread
From: Thomas Neumann @ 2005-07-07 13:42 UTC (permalink / raw)
  To: gcc-help

Hi,

I have a question concerning template specialization where gcc behaves 
different than other compilers. (I already asked this on 
comp.lang.c++.moderated, but got no response).

Is it possible to specialize just a member variable? (example code 
below). The C++ standard is a little bit vague on this or perhaps I just 
didn't get it. Existing C++ compilers seem to be undecided, gcc and 
Microsoft accept the sample code, while icc, Comeau and Borland reject it.

struct A {
     const A* a;
};

template <const char* c> struct B {
     static const A b;
};

extern const char c[1]="";
extern const char d[1]="";

template<> const A B<c>::b = {&B<d>::b};
template<> const A B<d>::b = {&B<c>::b};


I tend to think it is at least not allowed in this form (two
specialization referencing each other), at least icc gives a somewhat
convincing error message. Does anyone have an idea how to fix it? I want
to use template specialization to plug in information in a template
class, the different specializations need to reference each other. I do
not want to specialize the whole class, as most of it would be redundant.
Also I would like to add new specialization without changing header
files, which _seems_ to work using the approach shown above, but would
not work if I had to specialize the whole class.

Thomas

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
  2005-07-07 13:42 template specializiation Thomas Neumann
@ 2005-07-07 14:46 ` Eljay Love-Jensen
  2005-07-07 15:25   ` corey taylor
  0 siblings, 1 reply; 8+ messages in thread
From: Eljay Love-Jensen @ 2005-07-07 14:46 UTC (permalink / raw)
  To: Thomas Neumann, gcc-help

Hi Thomas,

Hmmm, unusual situation (for me).

Using...
http://www.comeaucomputing.com/tryitout/

I tend to trust (EDG's front end) Comeau C++'s error message (strict mode, -tused) on this one.

How to unravel this riddle, I'm not sure.  Sorry.

--Eljay

- - - - -
struct A
{
  const A* a;
};

template <const char* c>
struct B
{
  static const A b;
};

extern const char c[1]="";
extern const char d[1]="";

template<>
const A B<c>::b =
{
  &B<d>::b
};
template<>
const A B<d>::b = // Line 21
{
  &B<c>::b
};
- - - - - 

Comeau C/C++ 4.3.3 (Aug  6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing.  All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 21: error: explicit specialization of member
          "B<c>::b [with c=d]" must precede its first use
  const A B<d>::b = 
                ^

"ComeauTest.c", line 24: warning: parsing restarts here after previous syntax error
  };
   ^

1 error detected in the compilation of "ComeauTest.c".


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
  2005-07-07 14:46 ` Eljay Love-Jensen
@ 2005-07-07 15:25   ` corey taylor
  2005-07-07 15:51     ` Thomas Neumann
  0 siblings, 1 reply; 8+ messages in thread
From: corey taylor @ 2005-07-07 15:25 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: Thomas Neumann, gcc-help

This information from the ISO 14882 spec might help understand:

14.7.3.6

If a template, a member template or the member of a class template is
explicitly specialized then that specialization
shall be declared before the first use of that specialization that
would cause an implicit instantiation
to take place, in every translation unit in which such a use occurs;
no diagnostic is required. If the program
does not provide a definition for an explicit specialization and
either the specialization is used in a
way that would cause an implicit instantiation to take place or the
member is a virtual member function, the
program is ill-formed, no diagnostic required. An implicit
instantiation is never generated for an explicit
specialization that is declared but not defined. [Example:
template<class T> class Array { /* ... */ };
template<class T> void sort(Array<T>& v) { /* ... */ }
void f(Array<String>& v)
{
sort(v); //use primary template
// sort(Array<T>&), T is String
}
template<> void sort<String>(Array<String>& v); // error: specialization
// after use of primary template
template<> void sort<>(Array<char*>& v); // OK: sort<char*> not yet used
—end example]

corey

On 7/7/05, Eljay Love-Jensen <eljay@adobe.com> wrote:
> Hi Thomas,
> 
> Hmmm, unusual situation (for me).
> 
> Using...
> http://www.comeaucomputing.com/tryitout/
> 
> I tend to trust (EDG's front end) Comeau C++'s error message (strict mode, -tused) on this one.
> 
> How to unravel this riddle, I'm not sure.  Sorry.
> 
> --Eljay
> 
> - - - - -
> struct A
> {
>   const A* a;
> };
> 
> template <const char* c>
> struct B
> {
>   static const A b;
> };
> 
> extern const char c[1]="";
> extern const char d[1]="";
> 
> template<>
> const A B<c>::b =
> {
>   &B<d>::b
> };
> template<>
> const A B<d>::b = // Line 21
> {
>   &B<c>::b
> };
> - - - - -
> 
> Comeau C/C++ 4.3.3 (Aug  6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
> Copyright 1988-2003 Comeau Computing.  All rights reserved.
> MODE:strict errors C++
> 
> "ComeauTest.c", line 21: error: explicit specialization of member
>           "B<c>::b [with c=d]" must precede its first use
>   const A B<d>::b =
>                 ^
> 
> "ComeauTest.c", line 24: warning: parsing restarts here after previous syntax error
>   };
>    ^
> 
> 1 error detected in the compilation of "ComeauTest.c".
> 
> 
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
  2005-07-07 15:25   ` corey taylor
@ 2005-07-07 15:51     ` Thomas Neumann
  2005-07-07 15:56       ` corey taylor
  0 siblings, 1 reply; 8+ messages in thread
From: Thomas Neumann @ 2005-07-07 15:51 UTC (permalink / raw)
  To: corey taylor; +Cc: Eljay Love-Jensen, gcc-help

Hi,


> If a template, a member template or the member of a class template is
> explicitly specialized then that specialization
> shall be declared before the first use of that specialization that
but how would I do this? For functions this is easy, but I don't think I 
can declare a static member variable without defining it at the same 
time. I tried some combinations (using extern etc.), but always got 
compiler errors.

Thomas

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
  2005-07-07 15:51     ` Thomas Neumann
@ 2005-07-07 15:56       ` corey taylor
       [not found]         ` <42CD567F.2090702@users.sourceforge.net>
  0 siblings, 1 reply; 8+ messages in thread
From: corey taylor @ 2005-07-07 15:56 UTC (permalink / raw)
  To: Thomas Neumann; +Cc: Eljay Love-Jensen, gcc-help

Thomas,

Would this not work for you?

struct A
{
 const A* a;
};

template <const char* c>
struct B
{
 static const A b;
};

extern const char c[1]="";
extern const char d[1]="";

template<> const A B<d>::b;
template<> const A B<c>::b;

template<>
const A B<c>::b =
{
 &B<d>::b
};

template<>
const A B<d>::b = 
{
 &B<c>::b
};

corey

On 7/7/05, Thomas Neumann <tneumann@users.sourceforge.net> wrote:
> Hi,
> 
> 
> > If a template, a member template or the member of a class template is
> > explicitly specialized then that specialization
> > shall be declared before the first use of that specialization that
> but how would I do this? For functions this is easy, but I don't think I
> can declare a static member variable without defining it at the same
> time. I tried some combinations (using extern etc.), but always got
> compiler errors.
> 
> Thomas
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
       [not found]         ` <42CD567F.2090702@users.sourceforge.net>
@ 2005-07-07 16:39           ` corey taylor
  2005-07-07 19:34             ` Thomas Neumann
  2005-07-07 16:39           ` corey taylor
  1 sibling, 1 reply; 8+ messages in thread
From: corey taylor @ 2005-07-07 16:39 UTC (permalink / raw)
  To: Thomas Neumann, Eljay Love-Jensen; +Cc: gcc-help

Perhaps this should be filed as a bug on GCC since this should error
as Comeau does.

corey

On 7/7/05, Thomas Neumann <tneumann@users.sourceforge.net> wrote:
> Hi,
> 
> > Would this not work for you?
> >
> 
> > template<> const A B<d>::b;
> > template<> const A B<c>::b;
> >
> > template<> const A B<c>::b = { &B<d>::b };
> > template<> const A B<d>::b = { &B<c>::b };
> 
> hmm, perhaps it _should_ work :) Gcc and Comeau accept it, but Microsoft
> and Borland complain about multiple definitions of B<d>::b and B<c>::d.
> Perhaps I see compiler bugs here, but I would think that they are right.
> Any pointers to the standard?
> 
> Thomas
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
       [not found]         ` <42CD567F.2090702@users.sourceforge.net>
  2005-07-07 16:39           ` corey taylor
@ 2005-07-07 16:39           ` corey taylor
  1 sibling, 0 replies; 8+ messages in thread
From: corey taylor @ 2005-07-07 16:39 UTC (permalink / raw)
  To: Thomas Neumann; +Cc: gcc-help

Thomas,

  Sure.  What version of Visual Studio are you using as I don't know
that I've had to use this in my MSVC development yet.

14.7.3.15

An explicit specialization of a static data member of a template is a
definition if the declaration includes an
initializer; otherwise, it is a declaration. [Note: there is no syntax
for the definition of a static data member
of a template that requires default initialization.
template<> X Q<int>::x;
This is a declaration regardless of whether X can be default
initialized (8.5). ]

corey

On 7/7/05, Thomas Neumann <tneumann@users.sourceforge.net> wrote:
> Hi,
> 
> > Would this not work for you?
> >
> 
> > template<> const A B<d>::b;
> > template<> const A B<c>::b;
> >
> > template<> const A B<c>::b = { &B<d>::b };
> > template<> const A B<d>::b = { &B<c>::b };
> 
> hmm, perhaps it _should_ work :) Gcc and Comeau accept it, but Microsoft
> and Borland complain about multiple definitions of B<d>::b and B<c>::d.
> Perhaps I see compiler bugs here, but I would think that they are right.
> Any pointers to the standard?
> 
> Thomas
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: template specializiation
  2005-07-07 16:39           ` corey taylor
@ 2005-07-07 19:34             ` Thomas Neumann
  0 siblings, 0 replies; 8+ messages in thread
From: Thomas Neumann @ 2005-07-07 19:34 UTC (permalink / raw)
  To: corey taylor; +Cc: Eljay Love-Jensen, gcc-help

corey taylor wrote:
> Perhaps this should be filed as a bug on GCC since this should error
> as Comeau does.
this is PR 22354 now.

Thomas

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-07-07 19:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-07 13:42 template specializiation Thomas Neumann
2005-07-07 14:46 ` Eljay Love-Jensen
2005-07-07 15:25   ` corey taylor
2005-07-07 15:51     ` Thomas Neumann
2005-07-07 15:56       ` corey taylor
     [not found]         ` <42CD567F.2090702@users.sourceforge.net>
2005-07-07 16:39           ` corey taylor
2005-07-07 19:34             ` Thomas Neumann
2005-07-07 16:39           ` corey taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).