0

How can I write a templated typedef or using, such that

int arr[N];

is actually either

std::vector<int> arr(N);    /// C++03

or

std::array<int, N> arr;    /// C++11

I followed this answer. Can I write something similar to this

template <std::size_t N>
using int[N] = std::array<int, N>;

or a templated typedef

template <std::size_t N>
typedef int std::array<int, N> [N];

Also, I want the same with char[] and std::string. Is it possible ?

EDIT This is what I want to do

int arr[10];    // Declare an int array but use it as std::vector
arr.resize(20);
...   // Other methods from std::vector class
Community
  • 1
  • 1
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • 1
    What do you mean "such that `int arr[N];` is actually ..."? `int[N]` **is** `int[N]` – RamblingMad Jul 08 '15 at 11:22
  • @LightnessRacesinOrbit The `using` statement I followed from linked answer works well, but is not what I want. – Shreevardhan Jul 08 '15 at 11:22
  • 1
    @Shreevardhan this sounds like a case of the [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You have a problem with X, think that Y is the solution, so you ask about Y when you run into problems. What is the *actual* problem you are trying to solve? – Panagiotis Kanavos Jul 08 '15 at 11:26
  • 1
    Even after the edit, the question makes no sense - you are describing your attempted solution, NOT the actual problem. If you want a `vector`, `define a vector`. You should know though that `vector` creates a *new* int array internally when you call resize, copies the old data and switches it with the old internal copy. You can also use a `vector` wherever an `int[]` is required. So why this question? – Panagiotis Kanavos Jul 08 '15 at 11:28
  • @PanagiotisKanavos I wanted that if I declare an int array it should change to a vector. I basically want Java like syntax here. – Shreevardhan Jul 08 '15 at 11:30
  • 2
    @Shreevardhan: You're not listening. That is not what people are asking. People want to know _why_ you "want that if you declare an int array it should change to a vector". – Lightness Races in Orbit Jul 08 '15 at 11:31
  • why would you want to do it in the first place? – David Haim Jul 08 '15 at 11:32
  • If you could give `int` another meaning, how would you then express the actual `int` as in `int main`? – edmz Jul 08 '15 at 11:33
  • 1
    @Shreevardhan then just use a `vector`. That's what everyone who is going to read your code will expect. – Panagiotis Kanavos Jul 08 '15 at 11:33
  • Yes @PanagiotisKanavos I agree with what you say. I was only looking for ways to avoid writing `std::vector arr(N)` every time, making it shorter and simpler. – Shreevardhan Jul 08 '15 at 11:35
  • 2
    @Shreevardhan: If that is honestly your only use case here then I must say even considering this is a pretty terrible idea. – Lightness Races in Orbit Jul 08 '15 at 11:36
  • 4
    @Shreevardhan You may alias it to something simpler like `template using v_i = std::vector arr(N)`. – edmz Jul 08 '15 at 11:38
  • 2
    @black: Yeah, you could, if it were valid syntax. – Lightness Races in Orbit Jul 08 '15 at 11:51

1 Answers1

9

If you mean that you want to change the meaning of int[N] to actually be a std::vector or std::array

No, you can't do that.

Array-declarator notation like int[N] is completely fixed as a language construct and cannot be re-purposed.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055