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 6 3426 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 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-----
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
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
Thanks for all the informative replies, they were very helpful.
mich
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 -
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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){
|
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;
|
by: Shafik |
last post by:
Hello folks,
Whats the reason for the STL not allowing this:
vector<const stringv = vector<const string>();
--Shafik
| |
by: Javier |
last post by:
Hello,
thanks for the replies to my questions.
I have one more:
class A
{
public:
m1() const;
m2();
};
|
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
|
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)
{
|
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;
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |