Solved

Template Function

Posted on 2000-03-16
6
208 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 50 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now