473,721 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector<con st MyType> not allowed

I'm porting some code from windows to mac, and there are some
instances of std::vector<con st MyType>, that compiled just fine on the
pc, but won't compile under gcc.

I'd never tried to do this particular construct myself, and after some
searching online, I found a post somewhere saying this isn't legal c++
because the standard containers need types that are assignable.

My questions-- is that correct? Why does visual studio allow it if
it's wrong? And is there some other way to try to preserve the intent
of the original? (Otherwise I'm just going to strip out the
consts.)

Thanks for your time!

mich
Aug 11 '08 #1
6 3430
mu*******@yahoo .com wrote:
I'm porting some code from windows to mac, and there are some
instances of std::vector<con st MyType>, that compiled just fine on the
pc, but won't compile under gcc.

I'd never tried to do this particular construct myself, and after some
searching online, I found a post somewhere saying this isn't legal c++
because the standard containers need types that are assignable.

My questions-- is that correct? Why does visual studio allow it if
it's wrong? And is there some other way to try to preserve the intent
of the original? (Otherwise I'm just going to strip out the
consts.)
The general requirement is for the stored type to be assignable and
copy-constructible. But there can be cases where the assignable
requirement can be lifted. I can't think of one right now, but it is
not outside of the realm of possibility... For example, if you use the form

std::vector<myt ypev(10);

to create a vector of 10 objects, your 'mytype' should also be
default-constructible. But if you use the form

mytype prototype(somea rguments);
std::vector<myt ypev(10, prototype);

there is no requirement for default-constructibilit y...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 11 '08 #2
Sam
mu*******@yahoo .com writes:
I'm porting some code from windows to mac, and there are some
instances of std::vector<con st MyType>, that compiled just fine on the
pc, but won't compile under gcc.

I'd never tried to do this particular construct myself, and after some
searching online, I found a post somewhere saying this isn't legal c++
because the standard containers need types that are assignable.

My questions-- is that correct?
That is correct.
Why does visual studio allow it if
it's wrong?
Because the very last thing that Microsoft wants you to do is write portable
code that complies with commodity standards that are not controlled by
Microsoft. Microsoft would like nothing more than to force you to write code
that compiles only on Windows.
And is there some other way to try to preserve the intent
of the original? (Otherwise I'm just going to strip out the
consts.)
The "intent of the original" would be to declare the array as a plain,
garden-variety std::vector<MyT ype>, and, in functions or a context that
should not be able to modify the contents of the vector, pass it by
reference as a const std::vector<MyT ype&. Any code will have to access the
vector using const_iterator, not iterator (you'll probably have to rewrite a
whole bunch of stuff).
>
Thanks for your time!

mich
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAki gyhMACgkQx9p3GY HlUOK9vACdGVEZA 9MccaIz/azyhWgNhVxR
y/0An07qbB7Ni4cwZ VnwYU1ZXR4nxSb7
=vSnM
-----END PGP SIGNATURE-----

Aug 11 '08 #3
On Aug 12, 12:08 am, muzicm...@yahoo .com wrote:
I'm porting some code from windows to mac, and there are some
instances of std::vector<con st MyType>, that compiled just
fine on the pc, but won't compile under gcc.
I'd never tried to do this particular construct myself, and
after some searching online, I found a post somewhere saying
this isn't legal c++ because the standard containers need
types that are assignable.
My questions-- is that correct? Why does visual studio allow
it if it's wrong?
It's undefined behavior. It may cause an error when compiling,
it may compile, but do strange things during execution, or it
may work just fine.
And is there some other way to try to preserve the intent of
the original? (Otherwise I'm just going to strip out the
consts.)
What is the intent of the original? Given the semantics of
vector, having a non-const vector of const objects doesn't make
much sense. And if you declare an std::vector< MyType const,
there's no way you can get a non-const reference to any of the
elements.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 12 '08 #4
My questions-- is that correct? *Why does visual studio allow it if
it's wrong? *
MSVC probably allowed it because your code never uses a member
function that cared whether T was const or not. This is an example of
a struct that in one version allows a const whereas the other does not
template<class T>
struct Foo{
Foo(){}
#ifdef ALLOW_CONST_T
Foo(T const&v):var(v) {}
#else
Foo(T const&v){
var=v;
}
#endif
T var;
};

Whether I define ALLOW_CONST_T or not does not change the behavior of
Foo(other than performance in certian cases of T) but one version
allows Foo<const X and the other does not. And that only applies if
the user of Foo actually invokes that particular constructor.
So what is likely happening is that the particular mixture of user
code and how the container was implemented never actully hit upon
something that cared about the constness of T, whereas in the gcc
version it did.
Lance

Aug 12 '08 #5
Thanks for all the informative replies, they were very helpful.

mich
Aug 12 '08 #6
pjp
On Aug 11, 7:24*pm, Sam <s...@email-scan.comwrote:
*application_pg p-signature_part
< 1KViewDownload

muzicm...@yahoo .com writes:
I'm porting some code from windows to mac, and there are some
instances of std::vector<con st MyType>, that compiled just fine on the
pc, but won't compile under gcc.
I'd never tried to do this particular construct myself, and after some
searching online, I found a post somewhere saying this isn't legal c++
because the standard containers need types that are assignable.
My questions-- is that correct?

That is correct.
* * * * * * * * * * * * * * * * *Why does visual studio allow it if
it's wrong?

Because the very last thing that Microsoft wants you to do is write portable
code that complies with commodity standards that are not controlled by
Microsoft. Microsoft would like nothing more than to force you to write code
that compiles only on Windows.
Nice guess, if a bit paranoid, but you're wrong. Dinkumware added the
capability to declare a container of const type because -- are you
ready for this? -- customers demanded it. And now the latest revision
of the C++ Standard calls for *all* implementations to support vectors
of const type for all operations that make sense.

So much for vendor lock in.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
* * * * * * And is there some other way to try to preserve the intent
of the original? *(Otherwise I'm just going to strip out the
consts.)

The "intent of the original" would be to declare the array as a plain,
garden-variety std::vector<MyT ype>, and, in functions or a context that
should not be able to modify the contents of the vector, pass it by
reference as a const std::vector<MyT ype&. Any code will have to access the
vector using const_iterator, not iterator (you'll probably have to rewrite a
whole bunch of stuff).


Thanks for your time!
mich- Hide quoted text -

- Show quoted text -
Aug 12 '08 #7

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

Similar topics

11
1925
by: Lynn | last post by:
Hi, Is it better to use std::vector <type> with a pointer to the types being stored or the actual storage for the types ? In other words, is std::vector <mytype> or std::vector <mytype *> better ? I know that there are specific cases where it is dictated but in the general case, what do you think ? Thanks, Lynn McGuire
8
4873
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
6
5646
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. char temp; std::vector<unsigned charmmessage; while (!done){
4
2787
by: hn.ft.pris | last post by:
Hi: I've got the following simple code to test vector<const T>, #include <vector> #include <iostream> using namespace std; int main( void ){ vector<const intvec;
1
4905
by: Shafik | last post by:
Hello folks, Whats the reason for the STL not allowing this: vector<const stringv = vector<const string>(); --Shafik
1
4576
by: Javier | last post by:
Hello, thanks for the replies to my questions. I have one more: class A { public: m1() const; m2(); };
9
2244
by: t | last post by:
Can you use a plain iterator to vector<const intto write to it? It doesn't seem like it should be possible, but Visual C++ 2005 Express lets me: vector<const intvc; vc.push_back(5); vector<const int>::iterator iter = vc.begin(); *iter = 3; // compiles
12
5681
by: eiji.anonremail | last post by:
Hi all, I'm facing some uncertainty with const template arguments. Maybe someone could explain the general strategy. #include <vector> int main(int arc, char** argv) {
3
5644
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t k = i+1;
0
8840
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, 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...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9064
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 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...
0
5981
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();...
0
4484
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...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
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
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.