473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Const containers.

Which is the preferred way to create a const container in general:

1. container_type< value_type > const; or
2. container_type< value_type const >?

Jul 23 '05 #1
7 2211
"BigMan" <Bi****@abv.bg> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Which is the preferred way to create a const container in general:

1. container_type< value_type > const; or
2. container_type< value_type const >?

The latter should not compile.

So the only valid option is #2, e.g.:
std::vector<int> const cv( myArray, myArray+arraySize );
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2
This line compiles jsut fine:

std::vector< int const > v;

Jul 23 '05 #3
"BigMan" <Bi****@abv.bg> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
This line compiles jsut fine:

std::vector< int const > v;


Just as many illegal C++ constructs may happen to compile on
one or more existing platforms.
The ISO C++ standard says in 23.1/3 about the type of
the elements of a container:
<<The type of objects stored in these components must meet the requirements
of CopyConstructible types (20.1.3), and the additional requirements of
Assignable types.>>
'int const' obviously isn't a type that fulfills the requirement of being
assignable.

Besides, even if 'vector<int const>' were legal, it would have
a number of drawbacks compared to 'vector<int> const'.
A key difference is that vector<int> can implicitly be converted
to a 'vector<int> const&', but not to a 'vector<int const>&'.

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #4
OK, but why does the standard allow creating std::vector< int const >?
This could very easily be forbidden, e.g.:

template< typename value_type >
vector< value_tyep const >;

Jul 23 '05 #5
OK, but why does the standard allow creating std::vector< int const >?
This could very easily be forbidden, e.g.:

template< typename value_type >
vector< value_type const >;

Jul 23 '05 #6
"BigMan" <Bi****@abv.bg> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
OK, but why does the standard allow creating std::vector< int const >?
This could very easily be forbidden, e.g.:

template< typename value_type >
vector< value_tyep const >;


The standard does not allow it - but it doesn't either
explicitly require the use of that technique to prevent it.

There is a number of similar safeguards that the standard
committee could have included, but never bothered to.
For example, "mix-in" base classes such as the 'struct iterator'
template would best have a protected destructor, to avoid
dangerous deletion through a base class pointer.

The reality is that these benevolent people have limited
resources, and a lot of higher-priority tasks to deal with.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #7
In article <d7**********@news.hispeed.ch>,
"Ivan Vecerina" <IN*************************@vecerina.com> wrote:
"BigMan" <Bi****@abv.bg> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
OK, but why does the standard allow creating std::vector< int const >?
This could very easily be forbidden, e.g.:

template< typename value_type >
vector< value_tyep const >;
The standard does not allow it - but it doesn't either
explicitly require the use of that technique to prevent it.

There is a number of similar safeguards that the standard
committee could have included, but never bothered to.
For example, "mix-in" base classes such as the 'struct iterator'
template would best have a protected destructor, to avoid
dangerous deletion through a base class pointer.

The reality is that these benevolent people have limited
resources, and a lot of higher-priority tasks to deal with.


Agreed. But additionally the committee has little motivation to go
around requiring diagnostics on things that are likely to just fail at
compile time anyway. Especially if it is an area which could possibly
have extended functionality in a future standard. We do not want to
unnecessarily set up backwards compatibility problems for ourselves.

For example see:

http://www.open-std.org/jtc1/sc22/wg...fects.html#276

which will make the OP's code legal in C++0X if the container type
changes to std::list. It is even likely to work today on all
implementations. :-)

Indeed, I hope to see the requirements for a container's value_type
significantly relaxed for all of the containers in C++0X:

http://www.open-std.org/jtc1/sc22/wg...005/n1771.html
23.1 - Container requirements

-3- The type of objects stored in these components must meet the requirements
of MoveConstructible and MoveAssignable types. Additionally for some member
functions (as noted below), the types must meet the requirements of
CopyConstructible and/or CopyAssignable types...


These changes would not make vector<const T> legal, but they would allow
a greater range of T to be put in a vector (a const T is not
MoveAssignable).

Another possibility for today is:

std::tr1::array<const int, 3> v = {1, 2, 3};

If your implementation does not provide std::tr1::array, perhaps you
could use boost::array instead:

http://www.boost.org/doc/html/array.html

All that being said, a safe alternative today is still const vector<T>.
:-)

-Howard
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Calcote | last post by:
I'm not sure is there's a g++ specific newsgroup... g++ is telling me there's a const-related problem with my source code and I just don't see it. Now, I don't know that it means much, but VC7...
12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
2
by: Tim Partridge | last post by:
Why would this be illegal? #include <list> using namespace std; int main ( char argc, char* argv ) { list< const int * const > l; return 0; }
2
by: Alexander Malkis | last post by:
//Consider: class A { /*...*/ }; template<class T> class list {/*... */ }; void f(const list<const A*> lst) { /*...doesn't change the arg...*/ } void g(list<A*> lst) { f(lst);...
8
by: Nobody | last post by:
The requirement that STL container elements have to be assignable is causing me a problem. Consider a class X which contains both const and non-const data members: class X { public: X(const...
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
23
by: grubertm | last post by:
When accessing pointer member variables these are not treated as const even though the instance is const. This came as a complete surprise to me and I would appreciate some feedback why that is the...
2
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
16
by: subramanian100in | last post by:
Program 1: --------------- #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.