Hello,
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
#include <list>
typedef std::list < int[2] foo_t;
int main()
{
int v[2] = { 12, 34 };
foo_t bar;
bar.push_front( v);
return 0;
}
$ g++ -std=c++98 -Wall foo.cxx
stl_construct.h : In function `void std::_Construct (_T1*, const _T2&)
[with _T1 = int[2], _T2 = int[2]]':
stl_list.h:438: instantiated from `std::_List_nod e<_Tp>*
std::list<_Tp, _Alloc>::_M_cre ate_node(const _Tp&) [with _Tp = int[2],
_Alloc = std::allocator< int[2]>]'
stl_list.h:1163 : instantiated from `void std::list<_Tp,
_Alloc>::_M_ins ert(std::_List_ iterator<_Tp>, const _Tp&) [with _Tp =
int[2], _Alloc = std::allocator< int[2]>]'
stl_list.h:755: instantiated from `void std::list<_Tp,
_Alloc>::push_f ront(const _Tp&) [with _Tp = int[2], _Alloc =
std::allocator< int[2]>]'
foo.cxx:7: instantiated from here
stl_construct.h :81: error: ISO C++ forbids initialization in array new
Do I have to wrap the array in a struct?
#include <list>
struct sfoo { int v[2]; };
typedef std::list < sfoo foo_t;
int main()
{
sfoo s = { 12, 34 };
foo_t bar;
bar.push_front( s);
return 0;
}
Regards. 8 3422
On 2007-12-07 10:50:11 -0500, Spoon <root@localhost said:
>
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
Because arrays are neither copy constructible nor assignable.
>
Do I have to wrap the array in a struct?
Yes. Or, if you have TR1, use std::tr1::array , a template that defines
a fixed-size array that's copy constructible and assignable. It's also
slated for C++0x.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
Spoon wrote:
Hello,
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
#include <list>
typedef std::list < int[2] foo_t;
int main()
{
int v[2] = { 12, 34 };
foo_t bar;
bar.push_front( v);
return 0;
}
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
$ g++ -std=c++98 -Wall foo.cxx
stl_construct.h : In function `void std::_Construct (_T1*, const _T2&)
[with _T1 = int[2], _T2 = int[2]]':
stl_list.h:438: instantiated from `std::_List_nod e<_Tp>*
std::list<_Tp, _Alloc>::_M_cre ate_node(const _Tp&) [with _Tp = int[2],
_Alloc = std::allocator< int[2]>]'
stl_list.h:1163 : instantiated from `void std::list<_Tp,
_Alloc>::_M_ins ert(std::_List_ iterator<_Tp>, const _Tp&) [with _Tp =
int[2], _Alloc = std::allocator< int[2]>]'
stl_list.h:755: instantiated from `void std::list<_Tp,
_Alloc>::push_f ront(const _Tp&) [with _Tp = int[2], _Alloc =
std::allocator< int[2]>]'
foo.cxx:7: instantiated from here
stl_construct.h :81: error: ISO C++ forbids initialization in array new
Do I have to wrap the array in a struct?
#include <list>
struct sfoo { int v[2]; };
typedef std::list < sfoo foo_t;
int main()
{
sfoo s = { 12, 34 };
foo_t bar;
bar.push_front( s);
return 0;
}
You could use std::tr1::array if your implementation provides it.
Alternatively, there is boost::array.
Best
Kai-Uwe Bux
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
On 2007-12-07 21:59:30 -0500, Juha Nieminen <no****@thanks. invalidsaid:
Kai-Uwe Bux wrote:
>Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
The simplest answer is that that's the way it is in C. A more complex
answer is that you end up in a tangle, because the name of an array
decays to a pointer to its first element in many contexts, and when
you're down to a pointer you no longer know the size. Yes, C++ could
have added a bunch of rules about when you can and when you can't copy
them, but that would not be productive. If you need copyable arrays,
use std::tr1::array . For more details, see chapter 4 of my book, "The
Standard C++ Library Extensions."
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
On Dec 8, 7:59 am, Juha Nieminen <nos...@thanks. invalidwrote:
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
On Dec 8, 7:59 am, Juha Nieminen <nos...@thanks. invalidwrote:
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
I can try to justify but I am not sure if that was the real intent. It
could be, though.
This is probably an inherited concept from C. C arrays as well decay
to pointers. Consider this (or just about any string function in
<string.h>):
int strcmp ( const char * str1, const char * str2 );
It is a standard C(-style) string comparison function. Now, consider
that arrays did not decay to pointers and that they were copyable. In
the absence of overloading (and templates) which are C++ concepts, how
would you implement a strcpy function that could work with any length
C-string arrays? It don't think it would have been possible without
having functions like strcmp_1/strcmp_2/... and so on functions. Which
would be a real pain since you just can't write this function for all
possible array lengths.
Now, in C++, this is avoided with optimizations in case of templates.
Example: boost::lexical_ cast doesn't rely on such optimizations
though. It is forced that C-style string arrays decay to pointers
during instantiation of lexical_stream otherwise, this might cause
code-bloat. Consider the instantiations for as many C-string arrays as
different sized ones used in a program! In optimized modes, compilers
might do away with that many instantiations and have only one.
So, that (value semantics for arrays) is something that everyone wants
to avoid. Why have it?
On Dec 8, 5:33 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 8, 7:59 am, Juha Nieminen <nos...@thanks. invalidwrote:
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
On Dec 8, 7:59 am, Juha Nieminen <nos...@thanks. invalidwrote:
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
I can try to justify but I am not sure if that was the real intent. It
could be, though.
This is probably an inherited concept from C. C arrays as well decay
to pointers. Consider this (or just about any string function in
<string.h>):
int strcmp ( const char * str1, const char * str2 );
It is a standard C(-style) string comparison function. Now, consider
that arrays did not decay to pointers and that they were copyable. In
the absence of overloading (and templates) which are C++ concepts, how
would you implement a strcpy function that could work with any length
C-string arrays? It don't think it would have been possible without
having functions like strcmp_1/strcmp_2/... and so on functions. Which
would be a real pain since you just can't write this function for all
possible array lengths.
Now, in C++, this is avoided with optimizations in case of templates.
Example: boost::lexical_ cast doesn't rely on such optimizations
though. It is forced that C-style string arrays decay to pointers
during instantiation of lexical_stream otherwise, this might cause
code-bloat. Consider the instantiations for as many C-string arrays as
different sized ones used in a program! In optimized modes, compilers
might do away with that many instantiations and have only one.
So, that (value semantics for arrays) is something that everyone wants
to avoid. Why have it?
arrays are some part of paranoia that C++ iherits from C . C++ is the
result of rapid and uncuatious patching C with a linear algebraic
approach.
regards,
Fm.
On Dec 8, 3:33 pm, Abhishek Padmanabh
<abhishek.padma n...@gmail.comw rote:
[...]
It is a standard C(-style) string comparison function. Now,
consider that arrays did not decay to pointers and that they
were copyable. In the absence of overloading (and templates)
which are C++ concepts, how would you implement a strcpy
function that could work with any length C-string arrays?
The same way most other languages implement it? With the size
being an attribute of the array, and not part of its type? With
provisions for an open typed array, as well as a fixed length
array?
There are any number of solutions. Almost all superior to the
one chosen by C.
It don't think it would have been possible without having
functions like strcmp_1/strcmp_2/... and so on functions.
Ada has them. Java has them. In both languages, arrays are
real types, possibly passed by value, and obeying all of the
rules other types do.
--
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
On Dec 8, 5:33 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 8, 7:59 am, Juha Nieminen <nos...@thanks. invalidwrote:
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
On Dec 8, 7:59 am, Juha Nieminen <nos...@thanks. invalidwrote:
Kai-Uwe Bux wrote:
Built-in arrays do not satisfy the Assignable and CopyConstructib le concepts
required for types used in standard sequence containers.
Is there any technical or rational reason for this? After all, they
*are* copied and assigned if they are members of a struct/class. Why
can't it be so also when they are bare?
I can try to justify but I am not sure if that was the real intent. It
could be, though.
This is probably an inherited concept from C. C arrays as well decay
to pointers. Consider this (or just about any string function in
<string.h>):
int strcmp ( const char * str1, const char * str2 );
It is a standard C(-style) string comparison function. Now, consider
that arrays did not decay to pointers and that they were copyable. In
the absence of overloading (and templates) which are C++ concepts, how
would you implement a strcpy function that could work with any length
C-string arrays? It don't think it would have been possible without
having functions like strcmp_1/strcmp_2/... and so on functions. Which
would be a real pain since you just can't write this function for all
possible array lengths.
decay is for last decades just forget about it but being intrinsically
convertible to pointer is not conflicting with copy-constructabilit y
and assignability .numeric types are intrinsically convertible to each
other and that does not prevent copy or assign on either one.At the
time C was designed no neither OO was known nor GP and assignabilty/
convertibilty/... did not make sense so decay strategy was used .Why
should we be bound to restrictions that could be removed in a backward
compatible manor? I mean we can safely replace the idea of decay with
convertabiliy. All we need is a minor review.
regards,
FM. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Pemberton |
last post by:
I'm sure there's a good explanation for this effect, but I get rather
a strange output from this little test:
#include <iostream>
#include <list>
int main()
{
std::list<int> int_list;
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl list class must Ioverride in order for this
to work?
|
by: Eric Lilja |
last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope
it's readable at least):
#include <algorithm>
#include <iostream>
#include <vector>
class Row
{
public:
|
by: PengYu.UT |
last post by:
Hi,
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
|
by: Josh Mcfarlane |
last post by:
Just out of curiosity:
When would using std::list be more efficient / effective than using
other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for
anything.
Thanks,
Josh McFarlane
| |
by: alex221 |
last post by:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:
using std::list;
template < class Contents class Tree_node{
Contents cn;
list < BTree_node < Contents children;
........
|
by: Javier |
last post by:
Hi all,
I have this code:
class A
{
std::list<Bm_observadores;
void function()
{
|
by: Ray D. |
last post by:
Hey all,
I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:
maintainNeighbors.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<HostID, std::allocator<HostID&' from a
temporary of type 'std::list<HostID, std::allocator<HostID*'
helpers.cpp:99: error: in passing argument 1 of `void
|
by: isliguezze |
last post by:
template <class T>
class List {
public:
List();
List(const List&);
List(int, const T&);
void push_back(const T &);
void push_front(const T &);
void pop_back();
|
by: Isliguezze |
last post by:
Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:
template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
|
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:
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,...
|
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: 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...
|
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |