Solved

Template Function

Posted on 2000-03-16
6
212 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
FMX StringGrid1->Canvas->FillRect Problem 3 169
SNMP error No Such Object available on this agent at this OID 3 297
Unable to start eclipse ? 17 152
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

789 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