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 >? 7 2232
"BigMan" <Bi****@abv.b g> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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+arraySi ze );
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
This line compiles jsut fine:
std::vector< int const > v;
"BigMan" <Bi****@abv.b g> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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 CopyConstructib le 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
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 >;
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 >;
"BigMan" <Bi****@abv.b g> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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
In article <d7**********@n ews.hispeed.ch> ,
"Ivan Vecerina" <IN************ *************@v ecerina.com> wrote: "BigMan" <Bi****@abv.b g> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.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 MoveConstructib le and MoveAssignable types. Additionally for some member functions (as noted below), the types must meet the requirements of CopyConstructib le 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
doesn't see the problem either:
----snip-snip-snip----
#include <string>
#include <set>
|
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 board. Since
this function does not change anything in the class I declared it as
const. To count all pieces of a given color the functions iterates
through a "map" of CNode-pointers. "CNode" is another class that is
irrelevant to the problem.
|
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;
}
|
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); //Intuitively ok, but compiler rejects.
}
|
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 std::string &rStrId, int x) : m_strId(strId), m_x(x)
{
}
| |
by: recover |
last post by:
#include <string>
#include <iostream>
using namespace std;
class TConst
{
private:
string con;
string uncon;
public:
|
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 case.
Here's a snippet:
class MyClass {
public:
char *pChar;
};
void ChangePointedTo (char *ch)
|
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
{
|
by: subramanian100in |
last post by:
Program 1:
---------------
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |