473,385 Members | 1,798 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,385 software developers and data experts.

std::vector<const MyType> not allowed

I'm porting some code from windows to mac, and there are some
instances of std::vector<const 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 3399
mu*******@yahoo.com wrote:
I'm porting some code from windows to mac, and there are some
instances of std::vector<const 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<mytypev(10);

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

mytype prototype(somearguments);
std::vector<mytypev(10, prototype);

there is no requirement for default-constructibility...

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<const 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<MyType>, 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<MyType&. 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)

iEYEABECAAYFAkigyhMACgkQx9p3GYHlUOK9vACdGVEZA9Mcca Iz/azyhWgNhVxR
y/0An07qbB7Ni4cwZVnwYU1ZXR4nxSb7
=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<const 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 objektorientierter Datenverarbeitung
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_pgp-signature_part
< 1KViewDownload

muzicm...@yahoo.com writes:
I'm porting some code from windows to mac, and there are some
instances of std::vector<const 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<MyType>, 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<MyType&. 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
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 *>...
8
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
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. ...
4
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
by: Shafik | last post by:
Hello folks, Whats the reason for the STL not allowing this: vector<const stringv = vector<const string>(); --Shafik
1
by: Javier | last post by:
Hello, thanks for the replies to my questions. I have one more: class A { public: m1() const; m2(); };
9
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);...
12
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
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.