
April 20th, 2007, 10:25 AM
| | | MoreCRTP question
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 <typenameclass D>
class Base
{
protected:
Base(T x): i(x) {}
T i;
};
template<class T>
class Derived:public 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 | 
April 20th, 2007, 10:45 AM
| | | Re: MoreCRTP question
kostas wrote: Quote:
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 <typenameclass D>
class Base
{
protected:
Base(T x): i(x) {}
>
T i;
};
>
template<class T>
class Derived:public 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. | 
April 20th, 2007, 10:55 AM
| | | Re: MoreCRTP question
* kostas: Quote:
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 <typenameclass D>
class Base
{
protected:
Base(T x): i(x) {}
>
T i;
};
>
template<class T>
class Derived:public 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? | 
April 20th, 2007, 11:15 AM
| | | Re: MoreCRTP question
On Apr 20, 12:47 pm, "Alf P. Steinbach" <a...@start.nowrote: Quote:
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 Derived:public 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? | 
April 20th, 2007, 11:25 AM
| | | Re: MoreCRTP question
* kostas: Quote:
On Apr 20, 12:47 pm, "Alf P. Steinbach" <a...@start.nowrote: Quote:
>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 Derived:public 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? | 
April 20th, 2007, 11:35 AM
| | | Re: MoreCRTP question
On Apr 20, 1:18 pm, "Alf P. Steinbach" <a...@start.nowrote: Quote:
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. | 
April 20th, 2007, 11:45 AM
| | | Re: MoreCRTP question
Alf P. Steinbach wrote: Quote:
* kostas:
> Quote:
>On Apr 20, 12:47 pm, "Alf P. Steinbach" <a...@start.nowrote:
>> Quote:
>>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 Derived:public 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. | 
April 20th, 2007, 12:05 PM
| | | Re: MoreCRTP question
Ian Collins wrote: Quote:
Alf P. Steinbach wrote:
> Quote:
>>* kostas:
>> Quote:
>>>On Apr 20, 12:47 pm, "Alf P. Steinbach" <a...@start.nowrote:
>>>
>>>>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 Derived:public 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. | 
April 20th, 2007, 12:35 PM
| | | Re: MoreCRTP question
kostas wrote: Hi! Quote:
template<class T>
class Derived:public 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 :) Quote: |
Do you know some workaround?
| What do you think about :
template<class T>
class Derived:public Base<T, Derived>
{
Derived(T x):Base<T, ::Derived>(x){}
};
In this way according to me you are explicitly referring to the template
class and not the specific specialization Derived<T>.
Regards,
Zeppe | 
April 20th, 2007, 02:35 PM
| | | Re: MoreCRTP question
On Apr 20, 2:26 pm, Zeppe
<zeppe.remove.all.this.long.comm...@email.itwrot e: Quote:
What do you think about:
>
template<class T>
class Derived:public Base<T, Derived>
{
Derived(T x):Base<T, ::Derived>(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. | 
April 20th, 2007, 10:55 PM
| | | Re: MoreCRTP question
Zeppe wrote: Quote:
>
What do you think about:
>
template<class T>
class Derived:public Base<T, Derived>
{
Derived(T x):Base<T, ::Derived>(x){}
};
>
In this way according to me you are explicitly referring to the template
class and not the specific specialization Derived<T>.
>
| Nice one!
--
Ian Collins. | 
April 23rd, 2007, 06:35 PM
| | | Re: MoreCRTP question
On Apr 20, 5:16 am, kostas <skola...@gmail.comwrote: Quote:
Hi all
I encountered a problem testing a variation of CRTP.
(It's also proposed in "C++ Templates: The Complete Guide" byVandevoorde, Josuttis. See the MoreCurious template class in 16.3)
>
template<class T, template <typenameclass D>
class Base
{
protected:
Base(T x): i(x) {}
>
T i;
>
};
>
template<class T>
class Derived:public 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's a language issue: Using the unqualified name "Derived" finds the
injected class name, which can only be used to denote a type name (not
a bare template name; although you can append the template arguments). Quote: |
Do you know some workaround?
|
Yes, use a qualified name. In your example, use "::Derived", as in:
Derived(T x):Base<T, ::Derived>(x){}
I hope that helps,
Daveed |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|