0

I have a class _vector which I want to replace by std::vector in all instances of the code without manually changing the literal text of it. In a way, i want to replace the functionality of std::vector with a custom one which is a derived class of it.

My custom class is a one template parameter class which inherits from std::vector<T> where I implemented additional operator[] functionality. I want to replace all the std::vectors in the code by the custom one.

I tried to do

template <class T>
using vector = _vector<T>;

But when creating a std::vector it says that the call is ambiguous, which makes sense.

I also tried to use typedef in all posible combinations involving templates, with no avail, like so

template <class T>
typedef _vector<T> vector<T>;

I also tried to define std::vector as _vector, using

#define vector _vector

But it changes all the implementations of std::vector in _vector, even if I put this line after the class definition, as it is a preprocessor directive and is ran before compiling it.

I don't what more to do, and if someone has an answer, please share it. Thanks.

Garmekain
  • 664
  • 5
  • 18
  • You probably have `using namespace std;` at global scope. Remove it and redeclare your vector in your own namespace so names won't collide. Also identifies at global namespace beginning with underscore are reserved. – user7860670 Apr 23 '17 at 09:05
  • @VTT What does it affect? – Garmekain Apr 23 '17 at 09:07
  • 3
    Please stop trying to do this. Do not do this. Do not go down the path of making your code "magically" use some other type when `std::vector` is plainly written. Future maintainers are going to hate you--and you might be one of them. Just change your code to use "myvector" or whatever. Do not call it `_vector` because of this: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – John Zwinck Apr 23 '17 at 09:11
  • Well, I used _vector to be easier to read, in the code itself it has a valid name, and i dont collaborate with anyone, so thats not the problem. – Garmekain Apr 23 '17 at 09:32
  • Deriving from `std::vector` is a terrible idea to begin with. – Christian Hackl Apr 23 '17 at 09:33
  • @Christian why? – Garmekain Apr 23 '17 at 10:01
  • @user7908643: Generally, deriving from *anything* which isn't explicitly designed to be derived from is a terrible idea, in every programming language. There are countless in-depth explanations for this on Stack Overflow, for example here: http://stackoverflow.com/questions/6806173/subclass-inherit-standard-containers/ – Christian Hackl Apr 23 '17 at 10:09

0 Answers0