Template generated copy constructor

This is a discussion on Template generated copy constructor within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with a template class I'm writing. I suspect that what I want to do is impossible, ...

  1. #1
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141

    Template generated copy constructor

    I'm having a problem with a template class I'm writing. I suspect that what I want to do is impossible, but I just wanted to throw it out there and see if I'm just missing something.

    I'm writing a template that is effectively a container, and I need to be able to construct them from like containers with compatible (static_cast-able) data types.

    Something very much like this:
    Code:
    export template <typename t>
    class Wrapper
    {
        t WrappedObject;
    
    public:
        Wrapper();
        template <typename t2> Wrapper(const Wrapper<t2> &copy);
    };
    In the case where t and t2 are the same data type, I would think that this would generate the copy constructor. My testing shows that it doesn't, and I have to explicitly write the copy constructor.

    I strongly suspect that it isn't possible to generate a copy constructor by this method. Can someone show me a way to do it, or is it just plain impossible?
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You have to explicitly write a non-templated copy constructor. A template is never a copy constructor. The standard says so.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Well, that was fast.

    My hopes and dreams have never been shattered so quickly.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Where's the problem. It's just another constructor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    That was a joke.

    Perhaps I should use glBegin(GL_JOKE); and glEnd(); next time.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by AverageSoftware View Post
    Well, that was fast.

    My hopes and dreams have never been shattered so quickly.
    There is still, of course, a default copy constructor.

  7. #7
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by brewbuck View Post
    There is still, of course, a default copy constructor.
    Yes, but that doesn't work in the actual application. I need a deep copy.

    Like I said, I assumed that this wasn't possible, so my real project has both a copy constructor and the templated constructor. I just wanted to know if there was a clever workaround of some sort.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by AverageSoftware View Post
    Yes, but that doesn't work in the actual application. I need a deep copy.
    Ah, but if you always hold everything by value (no references or pointers), the default copy IS a deep copy

    Like I said, I assumed that this wasn't possible, so my real project has both a copy constructor and the templated constructor. I just wanted to know if there was a clever workaround of some sort.
    I suppose you could roll up the common copying actions into a template member function, and invoke that from the constructor. But that has all the normal problems associated with it of initialization inside the constructor body instead of the initialization list. It would break if you had reference members.

  9. #9
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by brewbuck View Post
    I suppose you could roll up the common copying actions into a template member function, and invoke that from the constructor. But that has all the normal problems associated with it of initialization inside the constructor body instead of the initialization list. It would break if you had reference members.
    The copy construction code is really quite trivial, the class is a dynamic matrix wrapper.

    I'm just always looking for ways to slim down the code, and this looked like a possibility.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 06:50 PM
  2. Replies: 9
    Last Post: 06-20-2008, 03:41 AM
  3. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM
  4. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 06:17 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21