dimanche 29 novembre 2015

A vector with members on the stack

I want this kind of interface

#include <vector>
void fun(std::vector<int> v){ }
int main(){
  fun({1, 2, 3, 4}); //<-- this type of invocation
}

instead of

void fun(int argc, int* args){ }
int main(){
   int a[]={1,2,3,4};
   fun(sizeof(a)/sizeof(int), a);
}

Can I make the vector go to the stack (or use something that behaves like an on-the-stack vector)?

(std::array appears to do the stack part, but it requires an explicit hardcoded size, and I don't want that.)

It's a premature optimization type of question, really, but I'm curious.

Aucun commentaire:

Enregistrer un commentaire