Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > MoreCRTP question

Reply
Thread Tools

MoreCRTP question

 
 
kostas
Guest
Posts: n/a
 
      04-20-2007
Hi all
I encountered a problem testing a variation of CRTP.
(It's also proposed in "C++ Templates: The Complete Guide" by
Vandevoorde, Josuttis. See the MoreCurious template class in 16.3)

template<class T, template <typename> class D>
class Base
{
protected:
Base(T x): i(x) {}

T i;
};

template<class T>
class Derivedublic Base<T, Derived>
{
Derived(T x):Base<T, Derived>(x){}
};

the problem is that in the Base ctor call (from within Derived ctor)
the Derived parameter is not recognized as class template. Old gcc
versions(3.3.3) could cope with this, but not any more. Other
compilers have also problem.

So is it a language or a compiler issue?
Do you know some workaround?
Any help is appreciated.

Kostas

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-20-2007
kostas wrote:
> Hi all
> I encountered a problem testing a variation of CRTP.
> (It's also proposed in "C++ Templates: The Complete Guide" by
> Vandevoorde, Josuttis. See the MoreCurious template class in 16.3)
>
> template<class T, template <typename> class D>
> class Base
> {
> protected:
> Base(T x): i(x) {}
>
> T i;
> };
>
> template<class T>
> class Derivedublic Base<T, Derived>
> {
> Derived(T x):Base<T, Derived>(x){}
> };
>
> the problem is that in the Base ctor call (from within Derived ctor)
> the Derived parameter is not recognized as class template. Old gcc
> versions(3.3.3) could cope with this, but not any more. Other
> compilers have also problem.
>
> So is it a language or a compiler issue?


It looks OK to me and Sun CC accepts it.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-20-2007
* kostas:
> Hi all
> I encountered a problem testing a variation of CRTP.
> (It's also proposed in "C++ Templates: The Complete Guide" by
> Vandevoorde, Josuttis. See the MoreCurious template class in 16.3)
>
> template<class T, template <typename> class D>
> class Base
> {
> protected:
> Base(T x): i(x) {}
>
> T i;
> };
>
> template<class T>
> class Derivedublic Base<T, Derived>
> {
> Derived(T x):Base<T, Derived>(x){}
> };
>
> the problem is that in the Base ctor call (from within Derived ctor)
> the Derived parameter is not recognized as class template. Old gcc
> versions(3.3.3) could cope with this, but not any more. Other
> compilers have also problem.
>
> So is it a language or a compiler issue?
> Do you know some workaround?
> Any help is appreciated.


I failed to find anything about it in the standard, but adding the magic
word "class" before "Derived" seems to do the trick.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
kostas
Guest
Posts: n/a
 
      04-20-2007
On Apr 20, 12:47 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> I failed to find anything about it in the standard, but adding the magic
> word "class" before "Derived" seems to do the trick.


if you mean this:
template<class T>
class Derivedublic Base<T, Derived>
{
public:
Derived(T x):Base<T, class Derived>(x){}

};

it doesn't work for me(gcc 3.4.4 or 4.1.1)
what compiler do you use?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-20-2007
* kostas:
> On Apr 20, 12:47 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>> I failed to find anything about it in the standard, but adding the magic
>> word "class" before "Derived" seems to do the trick.

>
> if you mean this:
> template<class T>
> class Derivedublic Base<T, Derived>
> {
> public:
> Derived(T x):Base<T, class Derived>(x){}
>
> };
>
> it doesn't work for me(gcc 3.4.4 or 4.1.1)
> what compiler do you use?


MSVC 7.1, but now that I checked it with Comeau Online it's evidently an
MS extension, not kosher, so no wonder I didn't find it in the standard.

I don't see any solution to the technical problem, how to refer to
Derived as a general non-specialized template within the Derived class.

However, it shouldn't be difficult to create a workaround at the design
level. E.g., simply move whatever functionality that Base uses in
Derived, to some other class, e.g. another base class of Derived. Or
perhaps you don't really need a template template parameter, perhaps all
that's needed is an ordinary type parameter.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
kostas
Guest
Posts: n/a
 
      04-20-2007
On Apr 20, 1:18 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> However, it shouldn't be difficult to create a workaround at the design
> level

Thanks for the response, but i wish i could use this design.
After all it's only a slight variation of a well known pattern.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-20-2007
Alf P. Steinbach wrote:
> * kostas:
>
>> On Apr 20, 12:47 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>>
>>> I failed to find anything about it in the standard, but adding the magic
>>> word "class" before "Derived" seems to do the trick.

>>
>>
>> if you mean this:
>> template<class T>
>> class Derivedublic Base<T, Derived>
>> {
>> public:
>> Derived(T x):Base<T, class Derived>(x){}
>>
>> };
>>
>> it doesn't work for me(gcc 3.4.4 or 4.1.1)
>> what compiler do you use?

>
>
> MSVC 7.1, but now that I checked it with Comeau Online it's evidently an
> MS extension, not kosher, so no wonder I didn't find it in the standard.
>

Did Comeau Online complain about the original?

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-20-2007
Ian Collins wrote:
> Alf P. Steinbach wrote:
>
>>* kostas:
>>
>>>On Apr 20, 12:47 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>>>
>>>>I failed to find anything about it in the standard, but adding the magic
>>>>word "class" before "Derived" seems to do the trick.
>>>
>>>if you mean this:
>>>template<class T>
>>>class Derivedublic Base<T, Derived>
>>>{
>>>public:
>>> Derived(T x):Base<T, class Derived>(x){}
>>>
>>>};
>>>
>>>it doesn't work for me(gcc 3.4.4 or 4.1.1)
>>>what compiler do you use?

>>
>>MSVC 7.1, but now that I checked it with Comeau Online it's evidently an
>>MS extension, not kosher, so no wonder I didn't find it in the standard.
>>

>
> Did Comeau Online complain about the original?
>

OK, I realise why the original should not compile now. I've used this
construct before with Sun CC, so I guess it's a compiler bug that lets
it compile!

--
Ian Collins.
 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      04-20-2007
kostas wrote:
> Hi all


Hi!


> template<class T>
> class Derivedublic Base<T, Derived>
> {
> Derived(T x):Base<T, Derived>(x){}
> };
>
> the problem is that in the Base ctor call (from within Derived ctor)
> the Derived parameter is not recognized as class template. Old gcc
> versions(3.3.3) could cope with this, but not any more. Other
> compilers have also problem.
>
> So is it a language or a compiler issue?


I think it's a language issue, even if I haven't read carefully the
standard to prove it

> Do you know some workaround?


What do you think about:

template<class T>
class Derivedublic Base<T, Derived>
{
Derived(T x):Base<T, :erived>(x){}
};

In this way according to me you are explicitly referring to the template
class and not the specific specialization Derived<T>.

Regards,

Zeppe
 
Reply With Quote
 
kostas
Guest
Posts: n/a
 
      04-20-2007
On Apr 20, 2:26 pm, Zeppe
<(E-Mail Removed)> wrote:
> What do you think about:
>
> template<class T>
> class Derivedublic Base<T, Derived>
> {
> Derived(T x):Base<T, :erived>(x){}
>
> };
>
> In this way according to me you are explicitly referring to the template
> class and not the specific specialization Derived<T>.


Thanks a lot. It works.
I think it also makes sense. So I hope it's not just another magic.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments