Tuesday, August 30, 2016

Digest for comp.lang.c++@googlegroups.com - 25 updates in 3 topics

Gareth Owen <gwowen@gmail.com>: Aug 30 07:05AM +0100

>> find multiple (sometimes over 10) same-named header files , most often of
>> different sizes. Whats the idea behind that, and how should I know which
>> one of those to include ?
 
The compiler knows, so you don't have to. If you want to include
stdio.h, use
 
#include <stdio.h>
 
> Ahhh welcome to linux. Actually there is no way to know.
 
There are lots of ways to know. The package manager will know,
 
> You can have different headers if you have installed different
> compilers or different versions of the same compiler, or just some
> hacks that didn't go well and left some headers... who knows?
 
Ask the package manager. The package manager knows.
 
Actually, none of Jacob's "answers" are the most likely - what he knows
about linux couldn't fill a hat. Doesn't stop him having opinions, of
course.
 
The most likely answer is that they all belong to one compiler.
On my system I have
 
/usr/include/stdio.h
/usr/include/c++/4.8/tr1/stdio.h
/usr/include/x86_64-linux-gnu/bits/stdio.h
 
all of which belong to the gcc version I have installed. (I also have
the ARM-versions for the ARM compiler, in folders with names like
arm-xilinx-eabi).
 
When I do
#include <stdio.h>
the compiler does the right thing.
"R.Wieser" <address@not.available>: Aug 30 11:12AM +0200

jacobnavia,
 
 
>Ahhh welcome to linux. Actually there is no way to know.
...
> Now, you can ask the compiler which headers is it using, if
> memory serves gcc had a command line option to show the
> paths its using.
 
Ah, that makes some sense. I seem to remember having seen something like
that (a reference to a cache of some sort).
 
Thanks for the hint.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
jacobnavia <jacob@jacob.remcomp.fr> schreef in berichtnieuws
nq28u0$al3$1@dont-email.me...
> Le 29/08/2016 à 21:16, R.Wieser a écrit :
> > And now I'm on a roll asking questions, one thing I did not expect was
to
> > find multiple (sometimes over 10) same-named header files , most often
of
> > different sizes. Whats the idea behind that, and how should I know
which
"R.Wieser" <address@not.available>: Aug 30 10:57AM +0200

Paavo,
 
> Anyway, if you have troubles with basic syntax like this,
> then I suggest to start with a proper textbook or tutorial.
 
Thanks, but al you seem to be saying is "go away". Thanks bub.
 
Is it really so hard to dereference something that it can't be described in
few lines ?
 
> But anyway, first google search shows there are things
> like objdump and readelf which might be useful for
> examining so-s.
 
Using 20-20 vision, sure. Mind you, I gave you a fair warning that I'm a
*beginner* (at least in respect to Linux and C{something} ). I *assume*
that library files are the .o files in the "lib" branch, but am not even
sure about that.
 
Also, how do you think I found that "nm" I spoke of ?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Paavo Helde <myfirstname@osa.pri.ee> schreef in berichtnieuws
vvidnecFA7cvB1nKnZ2dnUU78QXNnZ2d@giganews.com...
> On 29.08.2016 22:16, R.Wieser wrote:
> > Hello all,
 
> > Fair warning: I'm a novice in regard to using C++ (GCC) *and* using
linux
> > (lubuntu to be exact). If this is not the newsgroup to be asking
questions
 
> > - - - - - -
> > First problem, C++ related:
 
> > I need to transfer the address of a structure to a function, and have
only
> > be able to find a hackish solution for it. The problem is that the
variable
> > I've got is actually a pointer, and I have no idea how to dereference
it.
> > server = gethostbyname(argv[1]);
 
> > Now I want to transfer the address stored in the "server" variable to a
> > function. The only way I was able to do that was to refer to the first
(and
 
> > As someone wo has spend quite some time on Windows I'm rather accustomed
> > being able to pick a DLL and look inside it to see which functions it
> > exposes that I can use (yeah, I still have to generate a library from it
and
 
> > To be honest, I have no idea if the above is even feasible for C++ (GCC)
> > under linux.
 
> > I did find an example using "nm", but that only showed the contents of
the
 
> man fprintf
 
> for example.
 
> > And now I'm on a roll asking questions, one thing I did not expect was
to
> > find multiple (sometimes over 10) same-named header files , most often
of
> > different sizes. Whats the idea behind that, and how should I know
which
"R.Wieser" <address@not.available>: Aug 30 10:43AM +0200

嘱 Tiib,
 
> Yes, but it seemed that he wanted host addresses as strings
> from that hostent structure.
 
Nope, I just want the address of the structure itself. Thats all.
 
> it is not clear what programming language
 
I'm not sure myself, but I assume that its C++, seeing that let G++ compile
the sourcecode.
 
> and in what way he used on Windows.
 
I didn't. I'm doing it on a Linux variant.
 
Regards,
Rudy Wieser
 
 
-- Origional message
嘱 Tiib <ootiib@hot.ee> schreef in berichtnieuws
242b3c44-f5a2-4bed-b019-447827e5f856@googlegroups.com...
 
> > "R.Wieser" <address@not.available> spake the secret code
> > <57c4895a$0$871$e4fe514c@news.xs4all.nl> thusly:
 
> > >I need to transfer the address of a structure to a function, and have
only
> > >be able to find a hackish solution for it. The problem is that the
variable
> > >I've got is actually a pointer, and I have no idea how to dereference
it.
"R.Wieser" <address@not.available>: Aug 30 10:31AM +0200

Andy,
 
> As someone who started using Linux not many years ago
> after many years of other systems, which included a _lot_
> of Windows - I've almost never had to do that on Windows
 
:-) Who said I was using C{something} on Windows :-p
 
But consider my request both as an attempt to get a bit more knowledge about
how stuff works, as well as creating independance from the "they" that
should make the header and library files for me.
 
But, in the first place its only an attempt to generate a list with all
available functions that I can use in a program. Currently the only way to
know if a certain function (that I know exists in Windows) is available in
gcc/linux is by stumbling over it in an example program. And that isn't
acceptable to me.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Vir Campestris <vir.campestris@invalid.invalid> schreef in berichtnieuws
4vSdnVhKxMTWCFnKnZ2dnUU78afNnZ2d@brightview.co.uk...
> > As someone wo has spend quite some time on Windows I'm rather accustomed
> > being able to pick a DLL and look inside it to see which functions it
> > exposes that I can use (yeah, I still have to generate a library from it
and
"R.Wieser" <address@not.available>: Aug 30 10:09AM +0200

Richard,
 
> Here 'server' is a variable holding the address of a
> 'hostent' structure. server *is* the address.
 
I'm sorry, but I disagree with you. Just consider the difference between
 
struct hostent server;
 
and
 
struct hostent *server;
 
I regard your above statement true for the first, not the latter. As far as
I can tell for the latter "server" is the addres of the variable which holds
the address to the "hostent" structure (hence the need for dereferencing).
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Richard <legalize+jeeves@mail.xmission.com> schreef in berichtnieuws
nq21nb$ead$1@news.xmission.com...
 
> "R.Wieser" <address@not.available> spake the secret code
> <57c4895a$0$871$e4fe514c@news.xs4all.nl> thusly:
 
> >I need to transfer the address of a structure to a function, and have
only
> >be able to find a hackish solution for it. The problem is that the
variable
 
> "server is a pointer to a hostent structure"
 
> --
> "The Direct3D Graphics Pipeline" free book
<http://tinyurl.com/d3d-pipeline>
"R.Wieser" <address@not.available>: Aug 30 11:17AM +0200

Gareth,
 
> The compiler knows, so you don't have to. If you want
> to include stdio.h, use
 
> #include <stdio.h>
 
Thats exactly the problem. How do I know that I need to include stdio.h and
not foobar.h to be able to use a certain function ?
 
> There are lots of ways to know. The package manager will know,
 
What has the packet manager to do with anything ? *I'm* writing a program,
and *I* need to add the correct includes.
 
Regards,
Rudy Wieser
 
 
- Origional message:
Gareth Owen <gwowen@gmail.com> schreef in berichtnieuws
87wpiyokq8.fsf@gmail.com...
> jacobnavia <jacob@jacob.remcomp.fr> writes:
 
> > Le 29/08/2016 à 21:16, R.Wieser a écrit :
> >> And now I'm on a roll asking questions, one thing I did not expect was
to
> >> find multiple (sometimes over 10) same-named header files , most often
of
> >> different sizes. Whats the idea behind that, and how should I know
which
Ian Collins <ian-news@hotmail.com>: Aug 30 09:19PM +1200

On 08/30/16 09:17 PM, R.Wieser wrote:
 
>> #include <stdio.h>
 
> Thats exactly the problem. How do I know that I need to include stdio.h and
> not foobar.h to be able to use a certain function ?
 
man <certain function>
 
--
Ian
"R.Wieser" <address@not.available>: Aug 30 11:33AM +0200

Funny:
 
> In short: How do I dereference that "server" variable so
> the function receives a pointer to the structure ?
 
Odd: several replies, but none that actually told me how its done. Is it
some deep-dark secret that only masters of the trade are allowed to know ?
 
And for some of you guys here: don't try to second-guess what I'm after.
I'm in the habit of trying to ask my questions as exact as I can, needing it
to have *it* answered. (that does not mean that I do not appreciate further
hints in the same direction though!)
 
Do I really need to waste hours googeling hoping I will stumble over the
(most likely rather simple) answer ?
 
Regards,
Rudy Wieser
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 12:38PM +0300

On 30.08.2016 11:57, R.Wieser wrote:
 
>> Anyway, if you have troubles with basic syntax like this,
>> then I suggest to start with a proper textbook or tutorial.
 
> Thanks, but al you seem to be saying is "go away". Thanks bub.
 
I'm just saying that it is not productive to learn language basics from
a newsgroup, it's too tedious and chaotic. If you get stuck with some
more concrete problem then you can always come back here, and preferably
with a real code sample, not a vague description using wrong terms.
 
> Is it really so hard to dereference something that it can't be described in
> few lines ?
 
You wanted to get a pointer, that's basically the opposite of
dereferencing. Besides, your original question was answered immediately
by Richard so there was no need to repeat this answer.
 
If you have a pointer called 'server', then for dereferencing it you use
'*server' or 'server->', depending on the exact usage. For passing the
pointer without dereferencing you just use 'server'.
 
> *beginner* (at least in respect to Linux and C{something} ). I *assume*
> that library files are the .o files in the "lib" branch, but am not even
> sure about that.
 
No, library files are called .so and .a. I'm sorry I mentioned objdump
and readelf, I should have just said that examining the library symbols
is a rare need and definitely not something a beginner should do at all.
 
Cheers
Paavo
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 12:44PM +0300

On 30.08.2016 12:17, R.Wieser wrote:
 
>> #include <stdio.h>
 
> Thats exactly the problem. How do I know that I need to include stdio.h and
> not foobar.h to be able to use a certain function ?
 
From the function documentation of course. How else could you know how
to use the function at all?
 
If you want to use fprintf, then type man fprintf in the Linux command
line or google search and the correct #include is among the first things
it says:
 
fprintf(3) - Linux man page
Name
 
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf - formatted output conversion
 
Synopsis
 
#include <stdio.h>
 
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
 
NRHTH
Paavo
"Öö Tiib" <ootiib@hot.ee>: Aug 30 03:55AM -0700

On Tuesday, 30 August 2016 12:14:44 UTC+3, R.Wieser wrote:
 
> I regard your above statement true for the first, not the latter. As far as
> I can tell for the latter "server" is the addres of the variable which holds
> the address to the "hostent" structure (hence the need for dereferencing).
 
Unfortunately you seem to add one indirection that isn't there. In former
'server' is variable that contains all data of structure 'hostent'. To get
address to it you need to type '&server'.
 
In latter 'server' is pointer variable that can contain address of structure
'hostent'. When it is pointing at actual 'hostent' then you may dereference it.
For dereferencing you need to type '*server' (or 'server->' if you want access
particular member of it).
"R.Wieser" <address@not.available>: Aug 30 01:34PM +0200

Paavo,
 
> I'm just saying that it is not productive to learn language
> basics from a newsgroup, it's too tedious and chaotic.
 
Please, let *me* be the judge of that. This is a tiny bit of knowledge,
which, if solved, allows me to progress quite a bit.
 
> If you get stuck with some more concrete problem ...
 
I don't know how much more concrete it can become as what I described.
 
But maybe the following problem casing will help you:
 
two definitions:
 
1) struct hostent server1;
 
2) struct hostent *server2;
 
For *both* of the above I want to give a pointer to the actual hostent
structure to a function called "foobar", which expects the argument to be of
type unsigned char*.
 
Describe the calling method for *both*. (I'm rather sure that my answer
lies in the difference between the two).
 
Concrete enough for you ?
 
> and preferably with a real code sample, not a vague description ...
 
Thats what I posted and am using in my code. I'm sorry, but I have no idea
what "code samples" look like in your reality.
 
> ... using wrong terms.
 
See below:
 
> You wanted to get a pointer, that's basically the opposite of
> dereferencing.
 
And you talk about vague ?
 
Yes, I want to "get a pointer". But I want to move *towards* the structure,
not *away* from it. And as far as I'm aware thats indeed called
"dereferencing"
 
> Besides, your original question was answered immediately
> by Richard so there was no need to repeat this answer.
 
Bull. And if you think otherwise than quote it.
 
All he did was quoting the first line of my code sample. Yeah, thats
really helpfull. Not.
 
> then for dereferencing it you use '*server' or 'server->',
> depending on the exact usage
 
Also bull. Both
 
foobar( (unsigned char*) *server , ... )
 
as well as
 
foobar( (unsigned char*) server-> , ... )
 
throw a compile-time error (invalid cast for the first, unqualified
identifier for the second). And FYI, I have tried the former before posting
here (didn't think the latter having any chance of succeeding, so I didn't
even try it).
 
> No, library files are called .so and .a.
 
Thanks. So, I was looking in the wrong files. That could explain a lot.
:-)
 
Regards,
Rudy Wieser
 
 
 
-- Origional message:
Paavo Helde <myfirstname@osa.pri.ee> schreef in berichtnieuws
_LidnQTXd5SPyVjKnZ2dnUU78LPNnZ2d@giganews.com...
> more concrete problem then you can always come back here, and preferably
> with a real code sample, not a vague description using wrong terms.
 
> > Is it really so hard to dereference something that it can't be described
in
> >> like objdump and readelf which might be useful for
> >> examining so-s.
 
> > Using 20-20 vision, sure. Mind you, I gave you a fair warning that I'm
a
> > *beginner* (at least in respect to Linux and C{something} ). I
*assume*
"R.Wieser" <address@not.available>: Aug 30 01:48PM +0200

Ian,
 
> man <certain function>
 
:-\ I had not thought of that.
...
 
Just tested it, and I had ofcourse (among a few others) to choose a function
which clashes with a console command : write. :-( :-) Any idea what to do
in such cases ?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Ian Collins <ian-news@hotmail.com> schreef in berichtnieuws
e2l1dgFec6gU3@mid.individual.net...
> >> to include stdio.h, use
 
> >> #include <stdio.h>
 
> > Thats exactly the problem. How do I know that I need to include stdio.h
and
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 02:57PM +0300

On 30.08.2016 14:34, R.Wieser wrote:
>> basics from a newsgroup, it's too tedious and chaotic.
 
> Please, let *me* be the judge of that. This is a tiny bit of knowledge,
> which, if solved, allows me to progress quite a bit.
 
Why are not judging here anything, I express my opinions, you express yours.
 
> structure to a function called "foobar", which expects the argument to be of
> type unsigned char*.
 
> Concrete enough for you ?
 
Yes, that was concrete enough, thank you. Here are the answers to these
questions.
 
1) foobar(reinterpret_cast<unsigned char*>(&server1));
 
2) foobar(reinterpret_cast<unsigned char*>(server2));
 
(side note: this is C++ so you probably need to compile it with g++, not
gcc).
 
(side note 2: there is no dereferencing here, & is the address-of
operator. The term "dereferencing" has very specific meaning in C and
C++ and if you use it in a C or C++ forum everybody assumes you are
using it in the same meaning.)
 
 
>> and preferably with a real code sample, not a vague description ...
 
> Thats what I posted and am using in my code. I'm sorry, but I have no idea
> what "code samples" look like in your reality.
 
A proper code sample is preferably a complete source file including all
the #includes as well as int main(). This makes it easy for other people
to try to compile it and understand where the problems appear.
 
See e.g.
 
https://groups.google.com/forum/#!topic/comp.lang.c++/VaDCk_oujMg
"R.Wieser" <address@not.available>: Aug 30 02:01PM +0200

嘱 Tiib,
 
> In former 'server' is variable that contains all data of structure
> 'hostent'. To get address to it you need to type '&server'.
 
Agreed.
 
> In latter 'server' is pointer variable that can contain address
> of structure 'hostent'.
 
Again, agreed.
 
> When it is pointing at actual 'hostent' then you may dereference it.
 
YES. How do I do that ?
 
> For dereferencing you need to type '*server' (or 'server->' if you
> want access particular member of it).
 
In my first post I indicated I used the latter using the first member of the
structure, but I consider that to be a hack. I also indicated that I tried
"(*server)", and that it gave me a compile-time error.
 
Any other ideas ?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
嘱 Tiib <ootiib@hot.ee> schreef in berichtnieuws
6fe758df-bf42-430f-be92-88c7455378f3@googlegroups.com...
 
> > > Here 'server' is a variable holding the address of a
> > > 'hostent' structure. server *is* the address.
 
> > I'm sorry, but I disagree with you. Just consider the difference
between
 
> > and
 
> > struct hostent *server;
 
> > I regard your above statement true for the first, not the latter. As
far as
> > I can tell for the latter "server" is the addres of the variable which
holds
> > the address to the "hostent" structure (hence the need for
dereferencing).
> 'server' is variable that contains all data of structure 'hostent'. To get
> address to it you need to type '&server'.
 
> In latter 'server' is pointer variable that can contain address of
structure
> 'hostent'. When it is pointing at actual 'hostent' then you may
dereference it.
> For dereferencing you need to type '*server' (or 'server->' if you want
access
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:36PM

>being able to pick a DLL and look inside it to see which functions it
>exposes that I can use (yeah, I still have to generate a library from it and
>find the function declarations, but thats not the point).
 
$ nm -D /usr/lib64/libzip.so.2 | grep " T " |more
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:42PM


>> #include <stdio.h>
 
>Thats exactly the problem. How do I know that I need to include stdio.h and
>not foobar.h to be able to use a certain function ?
 
The man pages are your friend.
 
$ man -k printf
asprintf (3) - print to allocated string
dprintf (3) - print to a file descriptor
format (n) - Format a string in the style of sprintf
fprintf (3) - formatted output conversion
fprintf (3p) - print formatted output
fwprintf (3) - formatted wide-character output conversion
fwprintf (3p) - print formatted wide-character output
printf (1) - format and print data
printf (1p) - write formatted output
printf (3) - formatted output conversion
printf (3p) - print formatted output
snprintf (3) - formatted output conversion
snprintf (3p) - print formatted output
sprintf (3) - formatted output conversion
sprintf (3p) - print formatted output
swprintf (3) - formatted wide-character output conversion
...
 
$ man 3 printf | head -20
PRINTF(3) Linux Programmer's Manual PRINTF(3)
 
 
 
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion
 
SYNOPSIS
#include <stdio.h>
 
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
 
#include <stdarg.h>
 
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
 
Or you can use the appropriate standard document, for example:
 
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:45PM


>Just tested it, and I had ofcourse (among a few others) to choose a function
>which clashes with a console command : write. :-( :-) Any idea what to do
>in such cases ?
 
If you don't already know what manual section it is in, use
man -k to find all the relevent pages:
 
$ man -k fprintf
asprintf (3) - print to allocated string
dprintf (3) - print to a file descriptor
format (n) - Format a string in the style of sprintf
fprintf (3) - formatted output conversion
fprintf (3p) - print formatted output
fwprintf (3) - formatted wide-character output conve
 
Then use the section number:
 
$ man 3 fprintf
 
Section 1 is commands, 2 is system calls, 3 is library functions,
4 is device special files, etc.
 
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
Jerry Stuckle <jstucklex@attglobal.net>: Aug 30 08:47AM -0400

On 8/30/2016 8:01 AM, R.Wieser wrote:
 
> Any other ideas ?
 
> Regards,
> Rudy Wieser
 
Look at his update again. He isn't using the address of the first
member - he isn't referencing any member of the struct. You can take
the address of it with & and dereference it with *. For instance:
 
struct hostent server; // the actual structure
 
struct hostent* pserver = &server; // A pointer to the structure
 
At this level, it's really no different than other variables, i.e.
 
int i; // an int
int *ip = &i; // A pointer to an int
 
And you can use them just like other pointers, i.e.
 
void f(struct hostent * ps);
 
f(&server); // Use the address of the structure
f(pserver); // Use a pointer to the structure
 
Does this help?
 
And I agree with the others - you really need to find a good basic book
on C++ (or here, even C). It will help you much more than trying to
fumble your way through.
 
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:48PM

>> the function receives a pointer to the structure ?
 
>Odd: several replies, but none that actually told me how its done. Is it
>some deep-dark secret that only masters of the trade are allowed to know ?
 
Pointers are the most basic of concepts in C, and C++.
 
>hints in the same direction though!)
 
>Do I really need to waste hours googeling hoping I will stumble over the
>(most likely rather simple) answer ?
 
If you don't craft your question carefully, you won't get an useful answer.
 
The question, as originally posted, didn't really make sense in the context
of C or C++ programming. Casting your 'server' variable to an unsigned
char pointer is incorrect in either language.
Piotr Wyderski <piotr.wyderski.no.spam@gmail.com>: Aug 30 09:25AM +0200

Paavo Helde wrote:
 
> T*' are already non-portable enough so that additional cast to const C*
> would probably be the safest step here (assuming standard layout, T as
> the first member and equal sizeof-s).
 
Yes, you understood the use case correctly. It is all about bulk loading
-- I want to load a big chunk of disk data (not even memory-mapping the
file, full-blown asynchronous IO operations will be used instead) and
then apply some non-mutating operations on this array of Ts as if it
were an array of Cs. In the moment of content creation it was an array
of Cs, so technically it is a sequence of reinterpret_casts
C[]=>T[]=>C[], which just happened in different runs of the same
program. The content does not have to be portable, it will never leave
the machine. I just wanted it to be as C++-conforming as possible.
Since C++ appears to know something about the standard_layout
peculiarities, it seems doable without excessive violations of the standard.
 
Best regards, Piotr
"Öö Tiib" <ootiib@hot.ee>: Aug 30 03:30AM -0700

On Tuesday, 30 August 2016 10:26:08 UTC+3, Piotr Wyderski wrote:
> Since C++ appears to know something about the standard_layout
> peculiarities, it seems doable without excessive violations of the standard.
 
> Best regards, Piotr
 
Add something like that into code after that struct C is defined:
 
static_assert( std::is_standard_layout<C>::value
&& std::is_arithmetic<T>::value
&& std::is_same<T, decltype(C::t)>::value
&& sizeof (T[42]) == sizeof (C[42])
, "need to cast between T[] and C[]" );
 
That double-checks the situation in paranoid manner and so documents
why it is important and makes sure that some future maintenance does
not accidentally break it. Files on disk tend to live longer than versions
of software.
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Aug 30 12:28PM +0100

On Tue, 30 Aug 2016 09:25:59 +0200
> C++-conforming as possible. Since C++ appears to know something about
> the standard_layout peculiarities, it seems doable without excessive
> violations of the standard.
 
This looks like a very bad idea. If what you have committed to disk
is a binary representation of an array of T objects of some kind, you
cannot recreate your C objects from this in a portable fashion because
the C objects no longer exist. You could only do the reverse portably.
The operative word is "portably" - if sizeof C is the same as sizeof T
you will probably get away with it.
 
By virtue of the sixth bullet of §3.10/10 read with §9.2/20, given
a type T which enables struct C { T t; int i;}; to be both an aggregate
and a standard layout struct, and an object of type T:
 
T t;
 
what you can do on my reading of the standard is this:
 
C* c = reinterpret_cast<C*>(&t); // OK so far, but be very afraid
T t2 = c->t; // this is all you can do with C
 
But there may be something else in the standard which precludes even
this.
 
Obviously this would not allow you to treat your array of T objects as
if it were an array of C objects.
 
The correct thing to do is to deal with your T objects as T objects.
 
Chris
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Aug 30 02:39AM +0100

int main(int argc, char* argv[])
{
/* Introducing "indexitor", a new C++ container... */
 
std::vector<int> v;
for (int i = 0; i < 15; ++i)
v.push_back(i + 700);
 
neolib::indexitor<std::string, std::size_t> idx;
 
idx.push_back(std::make_pair("first", 5));
idx.push_back(std::make_pair("second", 5));
idx.push_back(std::make_pair("third", 5));
 
/* idx is holding three elements which map to first 5,
second 5 and third 5 elements respectively of a foreign container. */
 
/* Now for primary use-case: modify foreign container: */
v.erase(v.begin() + 2); // remove third element
auto ii = idx.begin();
idx.update_foreign_index(idx.begin(), 5 - 1); // update indexitor
 
for (std::size_t i = 0; i < v.size(); ++i)
std::cout << i << ": " << idx.find_by_foreign_index(i)->first << " ->
" << v[i] << std::endl;
 
/* All indexitor index/update operations are worst-case O(lg N)
complexity. */
}
 
Output:
 
0: first -> 700
1: first -> 701
2: first -> 703
3: first -> 704
4: second -> 705
5: second -> 706
6: second -> 707
7: second -> 708
8: second -> 709
9: third -> 710
10: third -> 711
11: third -> 712
12: third -> 713
13: third -> 714
 
Download (at own risk as not yet fully tested) from:
 
https://github.com/FlibbleMr/neolib
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: