C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

valid C++: template as base class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Christof Meerwald
Guest





PostPosted: Thu Sep 28, 2006 7:38 pm    Post subject: valid C++: template as base class Reply with quote



I am wondering if the following code is valid C++:

template<class T>
struct A
{
static void f()
{ }
};

struct B
: public A<int>
{
static void f()
{
A::f(); // is this valid?
}
};

int main()
{
B::f();
}


gcc, Visual C++ 2003 and Digital Mars don't like it,
but Intel C++ and HP aCC accept it.

Can anyone tell me if this is valid C++ and why or why not.


Christof

--
http://cmeerw.org sip:cmeerw at cmeerw.org
mailto:cmeerw at cmeerw.org xmpp:cmeerw at cmeerw.org

....and what have you contributed to the Net?

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





PostPosted: Thu Sep 28, 2006 10:51 pm    Post subject: Re: valid C++: template as base class Reply with quote



Christof Meerwald ha scritto:
Quote:
I am wondering if the following code is valid C++:

template<class T
struct A
{
static void f()
{ }
};

struct B
: public A<int
{
static void f()
{
A::f(); // is this valid?
}
};

No, it's not correct. Should be A<int>::f();

Ganesh

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





PostPosted: Fri Sep 29, 2006 5:39 am    Post subject: Re: valid C++: template as base class Reply with quote



Christof Meerwald wrote:
Quote:
I am wondering if the following code is valid C++:

template<class T
struct A
{
static void f()
{ }
};

struct B
: public A<int
{
static void f()
{
A::f(); // is this valid?

The compiler doesn't know the 'T' type of A. You'd better use
A<int>::f();

Quote:
}
};

int main()
{
B::f();
}


gcc, Visual C++ 2003 and Digital Mars don't like it,
but Intel C++ and HP aCC accept it.

Can anyone tell me if this is valid C++ and why or why not.

You used a template name without the type, and this is an error.
Inheriting from a template class is acceptable in C++.

Michael


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





PostPosted: Fri Sep 29, 2006 5:41 am    Post subject: Re: valid C++: template as base class Reply with quote

On 28 Sep 2006 13:51:00 -0400, Alberto Ganesh Barbati
<AlbertoBarbati (AT) libero (DOT) it> wrote:

Quote:
Christof Meerwald ha scritto:
I am wondering if the following code is valid C++:

template<class T
struct A
{
static void f()
{ }
};

struct B
: public A<int
{
static void f()
{
A::f(); // is this valid?
}
};

No, it's not correct. Should be A<int>::f();

I don't see why. Could you please quote the relevant sections from the
standard?

--
Gennaro Prota

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gennaro Prota
Guest





PostPosted: Fri Sep 29, 2006 8:24 pm    Post subject: Re: valid C++: template as base class Reply with quote

On 29 Sep 2006 08:41:55 -0400, Alberto Ganesh Barbati
<AlbertoBarbati (AT) libero (DOT) it> wrote:

Quote:
Gennaro Prota ha scritto:
On 28 Sep 2006 13:51:00 -0400, Alberto Ganesh Barbati
AlbertoBarbati (AT) libero (DOT) it> wrote:

Christof Meerwald ha scritto:
I am wondering if the following code is valid C++:

template<class T
struct A
{
static void f()
{ }
};

struct B
: public A<int
{
static void f()
{
A::f(); // is this valid?
}
};
No, it's not correct. Should be A<int>::f();

I don't see why. Could you please quote the relevant sections from the
standard?


The rules (§14.6.1) that allow the name A to be used without a parameter
list to refer to a particular specialization of the template A are
restricted to the scope defining A or a specialization of A.

Hmm... 14.6.1/2 seems to back up your interpretation. However the
immediately following paragraph says (emphasys mine):

The injected-class-name of a class template or class template
specialization can be used either with or without a
template-argument-list *wherever it is in scope*.

It seems to me the intent is for A to be found as an (accessible)
injected class name in the scope of the derived class. Frankly, I'd be
surprised of the contrary.

--
Gennaro Prota

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





PostPosted: Sun Oct 01, 2006 2:58 pm    Post subject: Re: valid C++: template as base class Reply with quote

Gennaro Prota ha scritto:
Quote:

Hmm... 14.6.1/2 seems to back up your interpretation. However the
immediately following paragraph says (emphasys mine):

The injected-class-name of a class template or class template
specialization can be used either with or without a
template-argument-list *wherever it is in scope*.

It seems to me the intent is for A to be found as an (accessible)
injected class name in the scope of the derived class. Frankly, I'd be
surprised of the contrary.


I read it again and again, and I couldn't find a flaw in your reasoning.
Frankly, I am very surprised to know that the name A (without <>) is
visible in the scope of B, but it now seems apparent that my belief is
only justified by the repeated use of non-conformant compilers.

BTW, Comeau Online accepts the code and it adds to the other three
compilers reported by other posters (Intel C++, HP aCC and Visual C++
2005). In front of a good argument and the agreement of four of the most
conformant compilers on the market, it's difficult to argue anymore ;-)

Ganesh

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





PostPosted: Tue Oct 03, 2006 1:45 am    Post subject: Re: valid C++: template as base class Reply with quote

Christof Meerwald ha scritto:
Quote:

My understanding is that the rules (14.6.1) have slightly changed and C++ 98
didn't allow it, but the latest working draft
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2009.pdf) does
allow it. Does anyone know when this change was made (was this included in
TC1)?


Yes, they are already included in TC1.

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.