I am a newcomer to C++ and am reading a example code right now. In
this example code, the author used a lot of "vector<static DataType>
somename;". I know the static vector definition but wonder what's the
different between static vector and "vector<static xxx> xxx"? Can
anybody explain this for me? Thanks a lot.
Cuthbert
The usage
vector<static T> v;
is not valid standard C++.
If you're sure that the example was /exactly/ like that, then please
tell the group what book you're reading, and locate the nearest
fireplace.
There remains of course the possibility of some compiler-specific
language extension, and/or that you remember the example incorrectly.
Cheers & hth.,
- Alf
I tried to take off 'static' idenfier and compiled the source code. It
passed in VC2005pro and worked just like original code with 'static'.
Is it typo or something else? don't know.
Cuthbert
> I tried to take off 'static' idenfier and compiled the source code. It
> passed in VC2005pro and worked just like original code with 'static'.
> Is it typo or something else? don't know.
It's not standard C++, anyway. If you try to compile the code with gcc
or Comeau, you'll see that it will fail. Strange enough, it compiles
fine with VC even if you disable language extensions. Maybe it's a
compiler bug. You should ask in a newsgroup where VC-specific issues are
on-topic, e.g. microsoft.public.vc.language.
--
Christian Hackl
ha...@sbox.tugraz.at
And check the book's web site for errata, or email the publisher.
Another thing to remember is that a lot of schools use really old books
to teach C++, if they teach it at all. It could be some pre-standard
monstrosity.
What is std::vector<static something> supposed to do in VC?
I don't *think* it does anything, as this program compiles and runs
with no issues:
== BEGIN CODE ==
#include <cassert>
#include <vector>
#include <typeinfo>
using namespace std;
class A { };
int main () {
A a;
vector<A> as(1);
vector<static A> sas(1);
assert(typeid(as) == typeid(sas));
assert(typeid(as[0]) == typeid(sas[0]));
as.push_back(a);
sas.push_back(a);
}
== END CODE ==
It seems to be ignored. Also, FWIW, typeid(sas).name() and typeid(sas
[0]).name() return:
sas = class std::vector<class A,class std::allocator<class A> >
sas[0] = class A
I wonder what book he's reading.
Jason
Also it does not affect the storage duration of vector elements or
anything like that (was wondering if maybe a VC bug was about to be
revealed). The following program fills 3 vectors with [1 2 3], [4 5
6], and [7 8 9] respectively, and prints them to verify that that is
indeed their contents:
== BEGIN CODE ==
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int next () {
static int n;
return ++n;
}
ostream & operator << (ostream &s, const vector<int> &v) {
copy(v.begin(), v.end(), ostream_iterator<int>(s, " "));
return s << endl;
}
int main () {
vector<int> a(3);
vector<static int> b(3);
vector<static int> c(3);
generate(a.begin(), a.end(), next);
generate(b.begin(), b.end(), next);
generate(c.begin(), c.end(), next);
cout << a << b << c;
}
== END CODE ==
The expected output is produced:
1 2 3
4 5 6
7 8 9
This is VC 2008. I suppose it's a bug that it accepts invalid code.
Jason