Saturday, November 19, 2016

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

Paavo Helde <myfirstname@osa.pri.ee>: Nov 19 01:27AM +0200

On 18.11.2016 23:01, Richard wrote:
> Re: difference between reference and pointer, all I wanted to hear
> them say is "references can't be null". To me, that's the important
> part to understand, and that's the part they missed.
 
For me, the most important difference is that one is object and the
other is not. From the language viewpoint this is the most drastic
difference, if something is not an object, then it does not (logically)
have an address, it does not (logically) occupy memory, and in general
acts more like some kind of compile-time feature than a run-time entity.
 
OTOH, the point about not having "null references" is pretty arbitrary,
the language could have easily introduced an "alias for no object", and
it is easy to actually construct one via simple code which is UB only
because the standard says so. So while the non-nullness is an important
difference, it's not a fundamental one.
 
Cheers
Paavo
Ben Bacarisse <ben.usenet@bsb.me.uk>: Nov 19 11:43AM

> (logically) have an address, it does not (logically) occupy memory,
> and in general acts more like some kind of compile-time feature than a
> run-time entity.
 
The term pointer can refer to a value as well, so it's more accurate to
say that a pointer value *may* be stored in an object -- you can't
really say, as a general rule, that pointers *are* objects.
 
Conversely, the C++ standard declines to say whether a reference
variable takes up any storage (it is "unspecified") so in some rather
unhelpful sense a reference might be stored in an object. It's
unhelpful because, as you say, you can never do anything "object-like"
with it even if it is.
 
Interestingly, references have a lifetime which is determined by their
"storage duration". That's to make the description in the standard
simpler -- you can't infer that they really do have storage.
 
> object", and it is easy to actually construct one via simple code
> which is UB only because the standard says so. So while the
> non-nullness is an important difference, it's not a fundamental one.
 
It's also possible to construct invalid references without UB. The use
of them will be UB, of course, but the point is that being null is not
the only way a reference can be invalid.
 
This illustrates a problem with "what's the difference between.." and
"what's important about..." questions. In effect it can be a guessing
game if it really means "what do I think is the most important thing
about...". The best thing would be to ask very open questions to get
candidates to talk about subjects in a guided way so you can determine
how deeply they understand them. Even if they don't hit your "target
points", you will find out of they are clear thinkers with a good
grounding.
 
--
Ben.
Paavo Helde <myfirstname@osa.pri.ee>: Nov 19 10:49PM +0200

On 19.11.2016 13:43, Ben Bacarisse wrote:
 
> The term pointer can refer to a value as well, so it's more accurate to
> say that a pointer value *may* be stored in an object -- you can't
> really say, as a general rule, that pointers *are* objects.
 
In C++ an object is a region of memory. A pointer in C++ is a
first-class object.
 
Cheers
Paavo
Ben Bacarisse <ben.usenet@bsb.me.uk>: Nov 19 10:14PM

>> really say, as a general rule, that pointers *are* objects.
 
> In C++ an object is a region of memory. A pointer in C++ is a
> first-class object.
 
What does new return if not a pointer? Is nullptr not a pointer? Does
the unadorned term always refer to a pointer object in technical
conversation about C++?
 
--
Ben.
Paavo Helde <myfirstname@osa.pri.ee>: Nov 20 01:16AM +0200

On 20.11.2016 0:14, Ben Bacarisse wrote:
 
> What does new return if not a pointer? Is nullptr not a pointer? Does
> the unadorned term always refer to a pointer object in technical
> conversation about C++?
 
Re-reading your post I now see that this is just about terminology -
should the term "pointer" refer foremost to objects or values. Standard
says of course that "pointer" foremost means a type and there can be
both objects and values of that type. If there is a confusion one has to
clarify what is meant.
 
So, it appears I should have worded my claim more carefully: there are
objects of pointer types, but there are no objects of reference types.
 
Cheers
Paavo
Popping mad <rainbow@colition.gov>: Nov 19 06:04PM

When I did the mock up test example for the question on static vars,
I've run into a compiler issue I've never seen before and I haven't
been able to find a fix. I'm getting an error that says
 
[ruben@flatbush test_static]$ make
g++ -Wall -ggdb -pg -pthread -o testme test.o main.o
/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/gcrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:7: testme] Error 1
 
makefile
 
1 CXX:=g++
2 CXXFLAGS:=-Wall -ggdb -pg -pthread
3
4 LDFLAGS:=-L/usr/local/lib/mysql -lmysqlpp -lmysqlclient
5
6 testme : test.o main.o
7 ${CXX} ${CXXFLAGS} -o $@ test.o main.o
8
9 main.o : main.cpp
10 ${CXX} ${CXXFLAGS} -o $@ -c main.cpp
11
12
13 testo : test.cpp test.h
14 ${CXX} ${CXXFLAGS} -o $@ -c test.cpp
15
16 clean :
17 rm testme *.o make.deps
18 touch *.cpp *.h
19
20 include make.deps
21 make.deps: *.cpp ; gcc -M *.cpp >$@
~
 
test.cpp
 
18 #include "test.h"
19 #include <iostream>
20 namespace blah{
21 int A::here;
22 void A::read(int in){
23 A::here = in;
24 std::cout << A::here << std::endl;
25 }
26 }
 
~
 
test.h
 
1 namespace blah{
2 class A{
3 public:
4 static int here;
5 A(int in=0){
6 here = in;
7 };
8 void read(int);
9 };
10
11 }
~
main.cpp
:w
18 #include "test.h"
19 #include <iostream>
20 namespace std{
21
22 int main(int argc, char** argv){
23 blah::A* a = new blah::A{9};
24 cout << a->here << endl;
25 return 0;
26 }
27
28 }
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Nov 19 11:29AM -0700

On Sat, 19 Nov 2016 18:04:50 +0000 (UTC), Popping mad
>(.text+0x20): undefined reference to `main'
>collect2: error: ld returned 1 exit status
>make: *** [makefile:7: testme] Error 1
<snip>
> 26 }
> 27
> 28 }
 
It looks like you're defining 'std::main' instead of 'main'. Try
this:
 
18 #include "test.h"
19 #include <iostream>
20 using namespace std;
21
22 int main(int argc, char** argv){
23 blah::A* a = new blah::A{9};
24 cout << a->here << endl;
25 return 0;
26 }
 
Louis
ruben safir <ruben@mrbrklyn.com>: Nov 19 03:56PM -0500

On 11/19/2016 01:29 PM, Louis Krupp wrote:
> 25 return 0;
> 26 }
 
> Louis
 
that work, but it is disappointing. How can main exist out of a namespace?
Dombo <dombo@disposable.invalid>: Nov 19 10:54PM +0100

Op 19-Nov-16 om 21:56 schreef ruben safir:
>> 25 return 0;
>> 26 }
 
> that work, but it is disappointing. How can main exist out of a namespace?
 
In this case the main() function ends up in the global namespace, and
that is where the linker expects to find it.
 
Unless you explicitly specify otherwise functions, classes..etc end up
in the global namespace, which is perfectly legal in C++.
 
Generally it is not a good idea to put things in the std namespace like
you tried to do with the main() function. The std namespace is intended
for the standard library. If you put things in the std namespace you
might get conflicts with future versions of the standard library.
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Nov 19 02:57PM -0700

On Sat, 19 Nov 2016 15:56:54 -0500, ruben safir <ruben@mrbrklyn.com>
wrote:
 
>> 26 }
 
>> Louis
 
>that work, but it is disappointing. How can main exist out of a namespace?
 
It doesn't. It's in the global namespace.
 
See https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx.
 
Louis
Popping mad <rainbow@colition.gov>: Nov 19 10:37PM

On Sat, 19 Nov 2016 14:57:12 -0700, Louis Krupp wrote:
 
 
> It doesn't. It's in the global namespace.
 
> See https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx.
 
 
can't reach a microsoft site. I think it is /dev/null routed.
 
Do you can a C++ link? or a GCC?
ram@zedat.fu-berlin.de (Stefan Ram): Nov 19 09:57PM

Why does ::std::binary_search return a boolean?
Why doesn't it return an iterator to the position
found? Wouldn't more information sometimes be
helpful to the caller?
ram@zedat.fu-berlin.de (Stefan Ram): Nov 19 10:22PM

>What does new return if not a pointer?
 
I call that an address value.
 
»A value of a pointer type that is a pointer to or past
the end of an object represents the address of the first
byte in memory occupied by the object or the first byte
in memory after the end of the storage occupied by the
object, respectively.«
 
3.9.2p3.4
 
>Is nullptr not a pointer?
 
std::nullptr_t is a distinct type that is neither a pointer
type nor a pointer to member type; rather, a prvalue of this
type is a null pointer constant and can be converted to a
null pointer value or null member pointer value.
Paavo Helde <myfirstname@osa.pri.ee>: Nov 20 12:21AM +0200

On 19.11.2016 23:57, Stefan Ram wrote:
> Why doesn't it return an iterator to the position
> found? Wouldn't more information sometimes be
> helpful to the caller?
 
std::lower_bound, std::upper_bound
Popping mad <rainbow@colition.gov>: Nov 19 01:49AM

I've a declared static as a public member of an
object but it doesn't seem to show up in the compiler
 
/home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here'
 
 
If I put it on top it errors are a private member. When I move it
to public, it is not being seen
 
The three test case files look like this
 
 
test.cpp
 
18 #include "test.h"
19 #include <iostream>
20 namespace blah{
21 void A::read(int in){
22 A::here = in;
23 std::cout << here << std::endl;
24 }
25 }
 
 
test.h
 
1 namespace blah{
2 class A{
3 public:
4 static int here;
5 A(int in=0){
6 here = in;
7 };
8 void read(int);
9 };
10
11 }
~
 
main.cpp
18 #include "test.h"
19 #include <iostream>
20 namespace std{
21
22 int main(int argc, char** argv){
23
24 blah::A* a = new blah::A{9};
25 cout << a->here << endl;
26 return 0;
27 }
28
29 }
~
makefile
 
1 CXX:=g++
2 CXXFLAGS:=-Wall -ggdb -pg -pthread
3
4 LDFLAGS:=-L/usr/local/lib/mysql -lmysqlpp -lmysqlclient
5
6 test : test.o main.o
7 ${CXX} ${CXXFLAGS} -o testme test.o main.o
8
9 main.o : main.cpp
10 ${CXX} ${CXXFLAGS} -o main.o -c main.cpp
11
12 test.o : test.cpp test.h
13 ${CXX} ${CXXFLAGS} -c test.cpp
14
15 clean :
16 rm testme *.o make.deps
17 touch *.cpp *.h
18
19 include make.deps
20 make.deps: *.cpp ; gcc -M *.cpp >$@
~
 
I need an extern?
1 || g++ -Wall -ggdb -pg -pthread -o testme test.o main.o
2 || /usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../lib/gcrt1.o: In function `_start':
3 || (.text+0x20): undefined reference to `main'
4 || test.o: In function `blah::A::read(int)':
5 /home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here'
6 /home/ruben/src/test_static/test.cpp|23| undefined reference to `blah::A::here'
7 || main.o: In function `std::main(int, char**)':
8 /home/ruben/src/test_static/main.cpp|25| undefined reference to `blah::A::here'
9 || main.o: In function `blah::A::A(int)':
10 /home/ruben/src/test_static/test.h|6| undefined reference to `blah::A::here'
Ian Collins <ian-news@hotmail.com>: Nov 19 04:07PM +1300

On 11/19/16 02:49 PM, Popping mad wrote:
> 9 };
> 10
> 11 }
 
Who have declared A::here, but you haven't defined it anywhere.
 
--
Ian
Paavo Helde <myfirstname@osa.pri.ee>: Nov 19 10:30AM +0200

On 19.11.2016 3:49, Popping mad wrote:
> I've a declared static as a public member of an
> object but it doesn't seem to show up in the compiler
 
> /home/ruben/src/test_static/test.cpp|22| undefined reference to `blah::A::here'
 
See https://isocpp.org/wiki/faq/ctors#link-errs-static-data-mems
 
While there, consider reading the whole https://isocpp.org/faq
Popping mad <rainbow@colition.gov>: Nov 19 05:01PM

On Sat, 19 Nov 2016 10:30:14 +0200, Paavo Helde wrote:
 
> See https://isocpp.org/wiki/faq/ctors#link-errs-static-data-mems
 
> While there, consider reading the whole https://isocpp.org/faq
 
 
This is an excellent explanation, but I'm still not clear on this. I
think it is saying that a static has to be declared and defined within a
compilational unit. I'm not sure why, though. And I've asked before,
and read, but I'm still not clear on what a compilation unit is.
Paavo Helde <myfirstname@osa.pri.ee>: Nov 20 12:12AM +0200

On 19.11.2016 19:01, Popping mad wrote:
 
> This is an excellent explanation, but I'm still not clear on this. I
> think it is saying that a static has to be declared and defined within a
> compilational unit. I'm not sure why, though.
 
There are other things in C++ which are declared in a header file and
then defined in some translation unit. The prime example are functions
(member and non-member). That's just the way how C++ does things.
 
When defining a class static member in some cpp file, one tells the
compiler to reserve memory for it in an the data segment of this
translation unit, plus one also specifies the arguments instructing the
compiler how to construct it.
 
However, for some simpler scenarios the initialization of the class
static member can be written directly in the declaration, which is
probably the most confusing point here.
 
> And I've asked before,
> and read, but I'm still not clear on what a compilation unit is.
 
It is just whatever is compiled into a single object .o/.obj file.
 
hth
Paavo
ruben safir <ruben@mrbrklyn.com>: Nov 19 11:45AM -0500

On 11/18/2016 03:06 PM, bitrex wrote:
> "const."
 
> What compiler does this compile under? I'm surprised putting "const"
> after a function call like that isn't a syntax error.
 
gcc
 
ruben safir <ruben@mrbrklyn.com>: Nov 18 09:13PM -0500

On 11/16/2016 05:38 AM, Popping mad wrote:
 
> std::thread t(update_data_for_widget, w, std::ref(data) );
 
> Why can't you just send a reference?
 
> std::thread t(update_data_for_widget, w, &data );
 
nadah on this?
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 19 12:58PM +0100

On 16.11.2016 11:38, Popping mad wrote:
 
> to do this correctly he says we need to use ref and a reference wrapper
 
> std::thread t(update_data_for_widget, w, std::ref(data) );
 
> Why can't you just send a reference?
 
Because these arguments are not passed on directly to the thread
function: they're stored by value, and passed on later.
 
 
> std::thread t(update_data_for_widget, w, &data );
 
This is not a reference, it's a pointer.
 
A pointer would work fine, it can be stored by value, but then the
thread function signature needs to be adjusted accordingly.
 
 
Cheers & hth.,
 
- Alf
Manfred <noname@invalid.add>: Nov 19 04:59PM +0100

On 11/19/2016 12:58 PM, Alf P. Steinbach wrote:
 
>> Why can't you just send a reference?
 
> Because these arguments are not passed on directly to the thread
> function: they're stored by value, and passed on later.
 
Reminds me of a similar question I had:
http://thread.gmane.org/gmane.comp.gcc.help/49709
 
see also:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68784
 
I was also surprised; in principle the language semantics could allow
using a reference type, but this is intentionally disabled by the
standard for std::thread - you have to explicitly use std::ref().
Apparently this was motivated by 'helping' the programmer not pass data
by reference accidentally between threads.
I am not sure I like this approach, though. I think that it is a basic
knowledge of any skilled C++ programmer to know how to use references.
 
What I like of C++ is its solid foundation of self-consistent language
logic. Introducing this type of exception to the basic semantics is not
really attractive to me.
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Nov 18 04:53PM -0700

On Fri, 18 Nov 2016 15:03:39 -0800 (PST), Shivu <rout.jyoti@gmail.com>
wrote:
 
 
>va[i].p = 0;
>va[i].d = 100000000;
 
>Is va a 2-D array? How to initialize pedestrian id and distance as elements in that array?
 
va is a one-dimensional array of structures. The structure has two
elements, passenger ID p and distance from vehicle to passenger d.
 
This might be an impertinent question, but have you had experience
with arrays, especially arrays of structures?
 
Louis
Paavo Helde <myfirstname@osa.pri.ee>: Nov 19 01:34AM +0200

On 18.11.2016 19:19, Prroffessorr Fir Kenobi wrote:
 
> the question is how it should look like, as i said in another thread
> i could use some easy convention to deniote blocks in file, 32 bit would be 4 ascii characters for making block signature then next 32 bit would be its
> size, like
 
If you are making a new binary format today, then it should fully
support 64-bit. There is no reason to limit sizes to 32 bits.
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: