Re: forward declaring std::vector. Works, but is it legal and standard compliant?
On Oct 28, 7:22 am, Jerry Coffin <jcof...@taeus.com> wrote:
I can't find anything to explicitly allow you to declare std::vector, so
I believe the code has undefined behavior.
Which means that ideally the standard should provide these forward
declarations in a header, I guess? Or is there a client solution to
this problem that is satisfactory? Or is the potential benefits from
not having to include the header deemed insignificant?
A simple typedef cannot be forward-declared, but obviously you could
encapsulate the std container inside a user type:
// Client header
struct std_vector;
struct Foo
{
void bar (std_vector const& v);
};
// Client implementation
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
using namespace std;
struct std_vector
{
vector <int> v;
};
void Foo::bar (std_vector const& v)
{
copy (v.v.begin (), v.v.end (),
ostream_iterator <int, char> (cout, " "));
}
int main()
{
// By exploiting initialization lists
// we avoid duplicating constructors.
int k [] = {3, 4};
std_vector v = {vector <int> (k, k + 2)};
Foo foo;
foo.bar (v);
}
Regards,
Vidar Hasfjord
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]