Solved

Template Function

Posted on 2000-03-16
6
210 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

823 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