GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Sep-2008, 07:58
dklovedoctor dklovedoctor is offline
New Member
 
Join Date: Aug 2008
Posts: 7
dklovedoctor is on a distinguished road

Template inside another template


Hallo all!
I have done some Google research, but i have not found something on this:

let us assume that,
in NsDeque.h i have:
CPP / C++ / C Code:
template <class TemplType>
class  NsDeque : public std::deque<TemplType>  {
...
}
in PsConstraint.h i have:
CPP / C++ / C Code:
template <class T>
class PsConstraint : public Constraint<T> {
   public:
      template <class U>
      class Groupe {
      ...
      }
   ...
   private:
   NsDeque<Groupe<U>> nsGrpedU;
   ...
}

the compiler (g++) points an error at
CPP / C++ / C Code:
NsDeque<Groupe<U>> nsGrpedU;
why is this so???
Is not nested call of templates supported?
Thanks in advance
Last edited by LuciWiz : 01-Sep-2008 at 07:59. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Sep-2008, 08:06
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: template inside another template


I didn't have the time to test it, perhaps you could provide us with the error message.

For sure the compiler will have problems with this line
CPP / C++ / C Code:
NsDeque<Groupe<U>> nsGrpedU;

//there should be a space between the closing angular braces
NsDeque<Groupe<U> > nsGrpedU;

The compiler will confuse the consecutive closing angular braces (">>") with the streaming operator.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 01-Sep-2008, 10:37
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Template inside another template


Quote:
Originally Posted by dklovedoctor
Is not nested call of templates supported?
Your code does not describe calls. Your code describes nested class declarations. The language standard allows the declaration of non-template classes to be nested, but the standard does not allow the declaration of template classes to be nested. All template classes have to be declared at file scope.

The question you need to ask yourself is what does nesting provide that can't also be more easily achieved through judicious use of namespace's.

When posting questions such as this, please provide either minimal code which when compiled will generate the same error, or at least post the error observed. Given what you posted, it is anyone's guess as to what you are really doing. The art of getting good answers lies in the ability to adequately communicate the problem observed.
Last edited by ocicat : 01-Sep-2008 at 11:08.
  #4  
Old 02-Sep-2008, 08:34
dklovedoctor dklovedoctor is offline
New Member
 
Join Date: Aug 2008
Posts: 7
dklovedoctor is on a distinguished road

Re: Template inside another template


As LuciWiz said it is the gap that caused the error.
I thank you both of you.
  #5  
Old 03-Sep-2008, 04:15
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Template inside another template


You have not did anything useful.
  #6  
Old 03-Sep-2008, 08:08
dklovedoctor dklovedoctor is offline
New Member
 
Join Date: Aug 2008
Posts: 7
dklovedoctor is on a distinguished road

Re: Template inside another template


Quote:
Originally Posted by Peter_APIIT
You have not did anything useful.

What do you mean by that?
  #7  
Old 03-Sep-2008, 11:21
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Template inside another template


Quote:
Originally Posted by dklovedoctor
What do you mean by that?

You're better off not to ask...there are a lot of wannabes around here and shrugging them off is a good skill to have.


Mexican Bob
  #8  
Old 05-Sep-2008, 08:04
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Template inside another template


Hi ocicat.

I am not sure I understand this statement:

Quote:
Originally Posted by ocicat
The language standard allows the declaration of non-template classes to be nested, but the standard does not allow the declaration of template classes to be nested.


I am not aware of the Standard disallowing nested template classes. Can you please clarify what you mean by this?

Thanks,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #9  
Old 05-Sep-2008, 08:37
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Template inside another template


Quote:
Originally Posted by LuciWiz
I am not aware of the Standard disallowing nested template classes. Can you please clarify what you mean by this?
My bad. The following code indicates where I was wrong:
CPP / C++ / C Code:
template <class T>
class foo {
public:
    template <class U>
    class bar {
    public:
    };
    bar<int> b;
};

int main() {
    foo<int> f;

    return 0;
}
Being able to nest template class declaration has not always been part of the language definition. In the early '90's, all template classes had to be defined at file scope. For anyone that has been programming in C++ for a long time, templates is a topic which has seen significant refinements since ~1999 in the language standard. Perhaps I have seen that template classes can now be nested, but when I responded earlier, I was remembering what was the norm of ~1992. Obviously, I was mistaken.

Irregardless of my memory lapse nesting has never been a common practice, & I stand by my earlier comment that using namespaces to accomplish the same scope limitation is perhaps a preferable practice.
  #10  
Old 05-Sep-2008, 09:03
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Template inside another template


Quote:
Originally Posted by ocicat
In the early '90's, all template classes had to be defined at file scope.

I was 10 back then, sorry

Quote:
Originally Posted by ocicat
Irregardless of my memory lapse nesting has never been a common practice, & I stand by my earlier comment that using namespaces to accomplish the same scope limitation is perhaps a preferable practice.

I feel the same way. Nested classes are useful if you draw some value from using the PIMPL idiom, but I can't think of any other useful feature on the spot.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fatal error C1083: Cannot open include file mia C++ Forum 5 14-May-2007 16:35
Template Problem Peter_APIIT C++ Forum 4 11-May-2007 03:17
Multiple questions for C++ project devster420 C++ Forum 1 20-Apr-2007 22:26
Template usage - global functions ankakusu C++ Forum 3 18-Dec-2006 16:18
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 07:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.