Solved

Template Function

Posted on 2000-03-16
6
213 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

679 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