Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6083561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:25:02+00:00 2026-05-23T11:25:02+00:00

I spent several hours, completely stuck when I realized that only the templated version

  • 0

I spent several hours, completely stuck when I realized that only the templated version of my code has a bug.

In the following code, when pushing_back elements in the myMap, the original vectors myVec1 and myVec2 are modified and contain garbage at the end of the execution. If I un-template everything, just replacing template<T> by double, then the code works fine as I would expect (the original arrays are untouched).

The funny thing is if I put a cout in the copy constructor, it does not get called if the code is templated. But it gets called if I replace the copy constructor with Vector<T2> by the original type Vector<T>, and then everything work fine.

Why wouldn’t the compiler know that T2==T since I only use double?

(note, the code has been made as short as possible so as to show the errors – I thus removed accessors, made everything public etc.).

#include <vector>
#include <map>


template<class T>
class Vector{
public:
    Vector():n(0),data(0){};
    Vector(int N):n(N),data(new T[N]){};
    Vector(T x, T y):n(2),data(new T[2]){data[0]=x; data[1]=y;};
    template<class T2> Vector(const Vector<T2>& rhs):n(rhs.n), data(new T[n])
    {
        for (int i=0; i<n; i++)
            data[i] = T(rhs.data[i]);
    }   
    ~Vector(){delete[] data;}

    Vector& operator=(const Vector& rhs)
    {
        if (rhs.n != n)
        {
            if (data)
                delete[] data;
            data = new T[rhs.n];
        }
        n = rhs.n;
        memcpy(data, rhs.data, n*sizeof(T));
        return *this;
    }
    T& operator[](int i){return data[i];}
    const T& operator[](int i) const {return data[i];}
    int n;
    T* data;
};

typedef  Vector<double> Vectord;

template <class T> inline bool operator<(const Vector<T>& v1, const Vector<T>& v2)
{
    for (int i=0; i<v1.n; i++)
    {
        if (v1[i]<v2[i]) return true;
        if (v1[i]>v2[i]) return false;
    }
    return false;
}


int main(int argc, char** argv)
{
    std::vector<Vectord> myVec1(3);
    myVec1[0] = Vectord(1.,3.);
    myVec1[1] = Vectord(3.,3.);
    myVec1[2] = Vectord(1.,5.);

    std::vector<Vectord> myVec2(3);
    myVec2[0] = Vectord(4.,1.);
    myVec2[1] = Vectord(2.,5.);
    myVec2[2] = Vectord(6.,5.);

    std::map<Vectord, std::vector<Vectord> > myMap;
    for (int i=0; i<3; i++)
    {
        myMap[myVec1[i]].push_back(myVec2[i]);
    }



    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:25:03+00:00Added an answer on May 23, 2026 at 11:25 am

    A templated constructor is never a copy constructor.

    So your class is using the automatically generated copy constructor.

    Cheers & hth.,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This afternoon I spent several hours trying to find a bug in my custom
I have spent several hours trying to find a means of writing a cross
I spent about 4 hours yesterday trying to fix this issue in my code.
I have spent several hours today reading up on doing Custom Routing in ASP.NET
Just spent several hours writing up for a new site... looks great in my
I hope someone can help me as I've already spent several hours trying to
I spent several hours reading through docs and forums, trying to find a solution
I have now spent several hours trying to figure this out. I have this
I just spent several hours of my life debugging this problem. I'm documenting it
I have spent the last several hours trying to get this to work and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.