struct Foo
{
void bar(std::vector<int, std::allocator<int> > const& v);
};
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
using namespace std;
void Foo::bar(std::vector<int> const& v)
{
copy(v.begin(), v.end(), ostream_iterator<int, char>(cout, " "));
}
int main()
{
vector<int> v;
v.push_back(3);
v.push_back(4);
Foo foo;
foo.bar(v);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
According to section 17.4.3.1.3/1: "Each name declared as an object with
external linkage in a header is reserved to the implementation to
designate that library object with external linkage, both in namespace
std and in the global namespace."
And according to section 17.4.3.1/3: "If the program declares or defines
a name in a context where it is reserved, other than as explicitly
allowed by this clause, the behavior is undefined."
I can't find anything to explicitly allow you to declare std::vector, so
I believe the code has undefined behavior.
--
Later,
Jerry.
The universe is a figment of its own imagination.
> In article <1193505682.3...@k79g2000hse.googlegroups.com>,
> firkr...@tlen.pl says...
>> namespace std {
>> template <typename T> class allocator;
>> template <typename T, typename A> class vector;
>> }
>
> According to section 17.4.3.1.3/1: "Each name declared as an object with
> external linkage in a header is reserved to the implementation to
> designate that library object with external linkage, both in namespace
> std and in the global namespace."
>
> And according to section 17.4.3.1/3: "If the program declares or defines
> a name in a context where it is reserved, other than as explicitly
> allowed by this clause, the behavior is undefined."
>
> I can't find anything to explicitly allow you to declare std::vector, so
> I believe the code has undefined behavior.
And, in particular, standard library implementations are allowed to add
extra template parameters to the standard templates, provided those
parameters have default values. So the forward declaration given above
may be wrong.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Please correct me, if I'm wrong here, but I think according to
the resolution of
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#94
it seems that it is *not* allowed for a library to do so, or
have I misunderstood something here?
Thanks and Greetings from Bremen,
Daniel
--
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
--
> On 28 Okt., 19:04, Pete Becker <p...@versatilecoding.com> wrote:
>> And, in particular, standard library implementations are allowed to add
>> extra template parameters to the standard templates, provided those
>> parameters have default values. So the forward declaration given above
>> may be wrong.
>
> Please correct me, if I'm wrong here, but I think according to
> the resolution of
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#94
>
> it seems that it is *not* allowed for a library to do so, or
> have I misunderstood something here?
>
You're right. Looks like I remembered it wrong.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
Yes, that.