Prev: Programatically rearranging icons
Next: utime() and GMT
From: Ben Voigt [C++ MVP] on 26 Oct 2009 13:03 > The message tells you what to do: > > template<typename Object> > typename Database<Object>::Record* // use typename > Database<Object>::Retrieve(const int n) > { > return &items[n]; > } > > That said, online Comeau compiles you code as is. (I have never really > understood when you need typename...). It's because you could have a specialization of Database in which Record isn't a struct: template<> class Database<int> { void Record() { StartRecording(); } }; In the example given, there's no ambiguity, stuff just doesn't work if Record isn't a type. But this line could either be a function call or a cast: Record(x); This could either be declaration or multiplication (if Record was a variable): Record* p; > > BTW, a better place for standard (non .NET) C++ questions is > > microsoft.public.vc.language Right. Cross-posting my comments there.
From: David Wilkinson on 26 Oct 2009 13:34 Ben Voigt [C++ MVP] wrote: >> The message tells you what to do: >> >> template<typename Object> >> typename Database<Object>::Record* // use typename >> Database<Object>::Retrieve(const int n) >> { >> return &items[n]; >> } >> >> That said, online Comeau compiles you code as is. (I have never really >> understood when you need typename...). > > It's because you could have a specialization of Database in which Record > isn't a struct: > > template<> > class Database<int> { void Record() { StartRecording(); } }; > > In the example given, there's no ambiguity, stuff just doesn't work if > Record isn't a type. But this line could either be a function call or a > cast: > > Record(x); > > This could either be declaration or multiplication (if Record was a > variable): > > Record* p; > >> >> BTW, a better place for standard (non .NET) C++ questions is >> >> microsoft.public.vc.language > > Right. Cross-posting my comments there. Ben: You think online Comeau is wrong to accept OP's original code? That would be bad news, because Comeau is my operational definition of whether a piece of code is correct or not :-). -- David Wilkinson Visual C++ MVP
From: Ben Voigt [C++ MVP] on 26 Oct 2009 13:41 "David Wilkinson" <no-reply(a)effisols.com> wrote in message news:u8VjCKmVKHA.220(a)TK2MSFTNGP02.phx.gbl... > Ben Voigt [C++ MVP] wrote: >>> The message tells you what to do: >>> >>> template<typename Object> >>> typename Database<Object>::Record* // use typename >>> Database<Object>::Retrieve(const int n) >>> { >>> return &items[n]; >>> } >>> >>> That said, online Comeau compiles you code as is. (I have never really >>> understood when you need typename...). >> >> It's because you could have a specialization of Database in which Record >> isn't a struct: >> >> template<> >> class Database<int> { void Record() { StartRecording(); } }; >> >> In the example given, there's no ambiguity, stuff just doesn't work if >> Record isn't a type. But this line could either be a function call or a >> cast: >> >> Record(x); >> >> This could either be declaration or multiplication (if Record was a >> variable): >> >> Record* p; >> >>> >>> BTW, a better place for standard (non .NET) C++ questions is >>> >>> microsoft.public.vc.language >> >> Right. Cross-posting my comments there. > > Ben: > > You think online Comeau is wrong to accept OP's original code? That would > be bad news, because Comeau is my operational definition of whether a > piece of code is correct or not :-). No, I think that in context, none of the non-typename uses could ever be allowed (multiplication and casting are statements, and not allowed at file or namespace scope). And I misread your comment "never understood when you need typename" as "why you need typename" and explained why it's needed sometimes. I don't see any good reason that this should be one of those times (the when). BTW, you did choose "force instantiation of all templates" when using tryitout, right? > > -- > David Wilkinson > Visual C++ MVP
From: David Wilkinson on 26 Oct 2009 14:00 Ben Voigt [C++ MVP] wrote: >> You think online Comeau is wrong to accept OP's original code? That >> would be bad news, because Comeau is my operational definition of >> whether a piece of code is correct or not :-). > > No, I think that in context, none of the non-typename uses could ever be > allowed (multiplication and casting are statements, and not allowed at > file or namespace scope). > > And I misread your comment "never understood when you need typename" as > "why you need typename" and explained why it's needed sometimes. I > don't see any good reason that this should be one of those times (the > when). > > BTW, you did choose "force instantiation of all templates" when using > tryitout, right? Ben: OK, I misread your post. Yes, I did use "force instantiation of all templates" (it seems to be the default). I understand that typename is sometimes needed to avoid ambiguity; it's just that it's hard to figure out when that might be. -- David Wilkinson Visual C++ MVP
From: Jeff Chang on 27 Oct 2009 17:29
Hi David, Thanks for your help. I will try the non-DotNet group. I also found out putting the "typename" up front fix the error message. By the way is this ANSI standard or just VS-2008? My textbook (year 2004) sample didn't have the "typename" in front. Besides hope you could give me some advice. Is there any benefit switching to .Net C++? I am interested in audio-visual programming in the long run. Should I even consider C# or Java for the sack of productivity? Thanks Jeff "Ben Voigt [C++ MVP]" <bvoigt(a)newsgroup.nospam> wrote in message news:82051108-F8AD-4832-A14E-060105CA0DE1(a)microsoft.com... >> The message tells you what to do: >> >> template<typename Object> >> typename Database<Object>::Record* // use typename >> Database<Object>::Retrieve(const int n) >> { >> return &items[n]; >> } >> >> That said, online Comeau compiles you code as is. (I have never really >> understood when you need typename...). > > It's because you could have a specialization of Database in which Record > isn't a struct: > > template<> > class Database<int> { void Record() { StartRecording(); } }; > > In the example given, there's no ambiguity, stuff just doesn't work if > Record isn't a type. But this line could either be a function call or a > cast: > > Record(x); > > This could either be declaration or multiplication (if Record was a > variable): > > Record* p; > >> >> BTW, a better place for standard (non .NET) C++ questions is >> >> microsoft.public.vc.language > > Right. Cross-posting my comments there. > > |