Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 20th, 2007, 10:25 AM
kostas
Guest
 
Posts: n/a
Default 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

  #2  
Old April 20th, 2007, 10:45 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #3  
Old April 20th, 2007, 10:55 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #4  
Old April 20th, 2007, 11:15 AM
kostas
Guest
 
Posts: n/a
Default 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?

  #5  
Old April 20th, 2007, 11:25 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #6  
Old April 20th, 2007, 11:35 AM
kostas
Guest
 
Posts: n/a
Default 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.

  #7  
Old April 20th, 2007, 11:45 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #8  
Old April 20th, 2007, 12:05 PM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #9  
Old April 20th, 2007, 12:35 PM
Zeppe
Guest
 
Posts: n/a
Default Re: MoreCRTP question

kostas wrote:
Quote:
Hi all
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
  #10  
Old April 20th, 2007, 02:35 PM
kostas
Guest
 
Posts: n/a
Default 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.


  #11  
Old April 20th, 2007, 10:55 PM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #12  
Old April 23rd, 2007, 06:35 PM
vandevoorde@gmail.com
Guest
 
Posts: n/a
Default 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

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles