?
Solved

Template Function

Posted on 2000-03-16
6
Medium Priority
?
219 Views
Last Modified: 2010-04-02
Hi

why in MS VC++ version 6 the following code dose not work !!!

template<typename T>
class shared_ptr
{
:
:
   template<typename Y>
   shared_ptr(const shared_ptr<Y>&r);
:
:
}  

Notice the differences between the template class's parameter (T) and the template function's parameter (Y).

Is there any work around ?

Tnx
0
Comment
Question by:ronenhh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2624898
No, there is no workaround.  (other than the obviuous, which is to write each function you need.)

VC 6.0 only has limited support for member template functions.  Actually they officially say they have no support at all for member template functions, but in some cases simple member template functions do compile.

I suspect that you will find that VC 7.0 will support member templates, but I don't know for sure and I don't know when it will be released.
0
 

Expert Comment

by:Yo081699
ID: 2624912
Apart the fact that class declaration ends with a ; which you forgot...
Your code works perfectly well under VC5.0
Didn't you make an error in the constructor definition:

template <typename T, typename Y>
shared_ptr<T>::shared_ptr(const shared_ptr<Y>& r)
{;}
0
 

Expert Comment

by:Yo081699
ID: 2624919
You have a too recent version of VC it seems!!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Expert Comment

by:nietod
ID: 2624936
If it works in VC 5, then it should work in VC 6.  Templates were greatly improved in VC 6, though still not up to standard.

What is the error that you are getting?  

We may need to see mor of your code, as Yo has sort of pointed out, there could be problems in the function definitions.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2627259
I had problems similar to this recently, and the only way I could get it to work was to define the member function template inside the class definition.

i.e. the following seems to work:

class MyClass
{
public:
    template <typename Y>
    MyClass(const Y& they) {}
};

int main(int argc, char* argv[])
{
    MyClass mc("anasca");
    return 0;
}

but if you define the body of the constructor outside the header it doesn't work:

class MyClass
{
public:
    template <typename Y>
    MyClass(const Y& they);
};

template <typename Y>
MyClass::MyClass(const Y& they)
{}

Nested templates like yours seem to work fine in the first instance too, but I couldn't work out exactly what the nested template definition would look like outside the class.
0
 
LVL 5

Accepted Solution

by:
pitonyak earned 150 total points
ID: 2628603

You requested a work around... The problem is that the current version of Microsofts compiler does not support member template functions.

One suggestion was that you include the template parameter to the class as well. This is a good idea under some circumstances... If the problem is that for each class instance you may have to have different template member functions.

Now I will assume that you can NOT have template member functions and that you require this functionality...

First, create a template function that is not a member function. Include a reference or a pointer to the desired class. Place the generic functionality here.

Now comes the hard part, how do you link the two items together? One option is to simply call the template function directly.

You could create a templat class which really does the work.

I had a similar problem, here is how I solved it.

I wanted the ability to stream most anything into a certain class. For some things I directly implemented the stream operator.

    LogFile& operator << (const char* text);
    //
    // And this allows me to stream almost anything :-)
    //
    LogFile& operator<<(const BaseCharConverter& temp)
        { *this << temp.data(); return *this; }

so I can stream text into my logfile as

log_file << "my text goes here";

Now, for the BaseCharConverter thing.

class BaseCharConverter
{
public:
    BaseCharConverter() : data_(0) {}
    virtual ~BaseCharConverter() { delete[] data_; }
    const char* data() const { return data_; }

protected:
    char* data_;
};

template<class T>
class CharConverter : public BaseCharConverter
{
public:
    CharConverter(const T& temp)
    {
        strstream s;
        s << temp << ends;
        data_ =  s.str();
    }
private:
    CharConverter();
};


Now I can stream anything that can be streamed to a standard stream as follows:

log_file << CharConverter<MyType>(x);

The secret here is that the template class is derived from a non-template class. This allows me to pass in a derived class... So, you could do something similar to the following:


class BaseWorker
{
public:
 virtual int do_work() = 0;
};

template<class T>
class Worker : public BaseWorker
{
public:
 virtual int do_work() {implement}
};

So, if you want to pass the data, then you probably need to do something like this...

 
 virtual int do_work(void* x) = 0;

Because you can NOT specify the real type in the base class...

virtual int do_work(void* x) {
    T* data = static_cast<T*>(x);
}

or something to this effect... I hope that this gives you some new ideas...

Andy





0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question