- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
following short example:
This code is working but what I really want something different.
[cpp]templateclass A { public: A():v_(T(0)){}; T get(){return v_;} private: T v_; }; template< template class Blub, typename T> T func(Blub &obj){ return obj.get(); } int main(int argc, char **argv) { A obj; func(obj); return 0; }[/cpp]
[cpp]template< templateclass Blub> T func(Blub &obj){ return obj.get(); } [/cpp]
gives:
tmplateMLPut2.cpp(12): error: identifier "T" is undefined
T func(Blub &obj){
With this I could instantiate func > and refering to "Modern C++ Design" this should work. I think I can handle my situation differently and the usenet gave me already a lot of tips but I want to understand why this is not working.
So here my questions: Is the last case valid C++? If yes, why is it not working?
Cheers
Patrick
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is invalid. The scope of a template parameter extends from its point of declaration until the end of its template so the scope of "T" will end after the declaration of "Blub".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your answer. Does this mean the best I can do is to define func like that:
The background of my question is the following: I have an Image class and some typedefs to, say BitImage, ByteImage, etc. and I want to write an ImageSender which is basically an adapter to a C-library to send images over a link. To send an image with the C-library I need 2 things depend on the type T of the image:
(img) which is imho unnatural because I want to send a
BitImage and it would be nicer to call sendImage(..).
Am I on a wrong way? Any ideas?
Cheers
Patrick
[cpp]templateand let the compiler deduce the type itselfclass Blub, typename T> T func(Blub &obj){ return obj.get(); }[/cpp]
[cpp]int main(int argc, char **argv) { A?obj; func(obj); return 0; }[/cpp]
The background of my question is the following: I have an Image
- a string representation of the image type. BitImage->"Bit", etc.... I solved this with specializations of templates:
[cpp]template <> const char * getImageTypeString
(){ return "Bit";} template <> const char * getImageTypeString (){ return "Byte";}[/cpp] - for every image type T i need to select a different C-function. This is solved the same way with partial specialization.
[cpp]templateWhen I have to give the type of sendImage I have to say sendImagevoid sendImage(const Image &img){ /*use T in getImageString and the send-function ...*/ }[/cpp]
BitImage and it would be nicer to call sendImage
Am I on a wrong way? Any ideas?
Cheers
Patrick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Compiler can deduce the type itself:
BitImage img;
sendImage(img); // Compiler will select getImageTypeString to return "Bit" if getImageTypeString called inside.
BitImage
sendImage(img); // Compiler will select getImageTypeString

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page