Monday, November 21, 2016

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

Christian Steins <cs01@quantentunnel.de>: Nov 21 06:08PM +0100

Am 20.11.2016 um 18:01 schrieb Öö Tiib:
 
> Linux, iOS and Android. Also there are more people who use it and
> so more job offers where Qt framework is used than where wxWidgets is
> used.
 
Sorry for the german OP, clicked on wrong group.
Anyway thanks for your answer. So, QT seems to be preferred over wx by
most c++ people.
 
Christian
scott@slp53.sl.home (Scott Lurndal): Nov 21 05:11PM


>Sorry for the german OP, clicked on wrong group.
>Anyway thanks for your answer. So, QT seems to be preferred over wx by
>most c++ people.
 
I think extrapolation from one to most is a stretch.
Juha Nieminen <nospam@thanks.invalid>: Nov 21 07:52AM

> 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.
 
A static member variable needs to exist somewhere in the executable
(because, among other things, you need to be able to eg. have a pointer
pointing to it). You have to tell the compiler which compilation unit
(in practice which object file) the actual instance of that variable is.
The compiler won't guess; you need to tell it explicitly where it should
be put.
 
(Incidentally, C++17 will introduce the concept of inline variables,
which will allow you to define static member variables in the header
file itself, causing the linker to merge all the instantiations into
one. But that will be available only after compilers start supporting
it.)
scott@slp53.sl.home (Scott Lurndal): Nov 21 01:26PM


>A static member variable needs to exist somewhere in the executable
>(because, among other things, you need to be able to eg. have a pointer
>pointing to it).
 
The confusing part for some folks is that gcc/binutils, when using -O3, won't
require the definition unless the address of the static const value
is taken. Remove -O3 and you'll start getting link errors.
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Nov 21 02:48PM

On Mon, 21 Nov 2016 13:26:07 GMT
> -O3, won't require the definition unless the address of the static
> const value is taken. Remove -O3 and you'll start getting link
> errors.
 
My recollection in the case of the declaration of a const static
integer member variable (typically with a literal value) is that you
don't need to provide a definition for it if it is not ODR-used,
whatever the optimization level. Taking an address is one way of
ODR-using it. Casting it to lvalue of another type is another. I don't
at the moment have the C++ standard in front of me to give you the
reference.
 
These days I guess you would probably declare such a static member
variable constexpr rather than const anyway.
ruben safir <ruben@mrbrklyn.com>: Nov 20 10:48PM -0500

What is a primary express in the context of this
 
37 int main(int argc, char **argv)
38 {
39 parent * tmp = new parent;
>> 40 parent * tmp2{ parent _a{tmp} } ;
41 //parent * tmp2 = new parent(tmp);
 
 
line 40 needs a primary expression before _a
woodbrian77@gmail.com: Nov 20 08:29PM -0800

On Sunday, November 20, 2016 at 9:48:25 PM UTC-6, ruben safir wrote:
> 38 {
> 39 parent * tmp = new parent;
> >> 40 parent * tmp2{ parent _a{tmp} } ;
 
How about this:
 
parent * tmp2{tmp};
 
?
 
Popping mad <rainbow@colition.gov>: Nov 21 07:07AM

What is a primary express in the context of this
Popping mad <rainbow@colition.gov>: Nov 21 07:09AM

On Sun, 20 Nov 2016 20:29:03 -0800, woodbrian77 wrote:
 
>> >> 40 parent * tmp2{ parent _a{tmp} } ;
 
> How about this:
 
> parent * tmp2{tmp};
 
aside from the fact that it doesn't address my question, It also doesn't
allocate anything.
Ian Collins <ian-news@hotmail.com>: Nov 21 08:17PM +1300

On 11/21/16 04:48 PM, ruben safir wrote:
>>> 40 parent * tmp2{ parent _a{tmp} } ;
> 41 //parent * tmp2 = new parent(tmp);
 
> line 40 needs a primary expression before _a
 
That it does, what on Earth are you trying to do there?
 
Please stick to one posting alias!
 
--
Ian
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Nov 21 01:17PM

On Sun, 20 Nov 2016 22:48:16 -0500
> >> 40 parent * tmp2{ parent _a{tmp} } ;
> 41 //parent * tmp2 = new parent(tmp);
 
> line 40 needs a primary expression before _a
 
You seem to be trying to learn C++ by trial and error with a compiler.
That is a hopeless way of trying to do it. You are getting
unnecessarily confused by the basics - in this case between a type and
a pointer to the type. You may also be confused by the syntactic
difference between a pointer and a reference.
 
Get yourself a decent book (or even a poor one would still be better
than trying to learn the language your way).
 
Assuming you and "Popping mad" are the same person and you are
referring to the parent class in his post "the new {} syntax", your
problem is that you are trying to initialize a parent* object 'tmp2'
with something other than an address of a parent object. (The syntax
is also invalid for other reasons, but the foregoing appears to be the
principal source of your confusion.) To take an address of an object
you use the '&' operator.
 
Sticking to a single identity would also serve to reduce the confusion
you may induce in your readers.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 21 03:03PM +0100

On 21.11.2016 04:48, ruben safir wrote:
>>> 40 parent * tmp2{ parent _a{tmp} } ;
> 41 //parent * tmp2 = new parent(tmp);
 
> line 40 needs a primary expression before _a
 
A primary expression is an expression.
 
A declaration -- what you have placed within the curly braces -- is not
an expression.
 
 
Cheers & hth.,
 
- Alf
Popping mad <rainbow@colition.gov>: Nov 21 03:10AM

why doesn't this enter the second constructor?
 
18 #include<iostream>
19
20 class parent{
21 public:
22 parent():_something{nullptr}, _val{0} {
23 std::cout << "null constructor\n";
24 };
25 parent(parent *in):_something{in}, _val{10} {
26 std::cout << "here\n";
27 _something->_val = 0;
28 };
29 parent * _something;
30 int _val;
31 };
32 void discover(parent * in){
33 std::cout<< in->_val << std::endl;
34 std::cout<< in->_something->_val << std::endl;
35 }
36
37 int main(int argc, char **argv)
38 {
39 parent * tmp = new parent;
40 parent * tmp2{tmp};
41 //parent * tmp2 = new parent(tmp);
42 discover(tmp2);
43
44
45
46 return EXIT_SUCCESS;
47 }
[ruben@flatbush max_path]$ g++ -Wall test.cpp
[ruben@flatbush max_path]$ ./a.out
null constructor
0
Segmentation fault
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Nov 20 09:06PM -0700

On Mon, 21 Nov 2016 03:10:01 +0000 (UTC), Popping mad
>null constructor
>0
>Segmentation fault
 
Because "parent * tmp2{tmp}" isn't constructing a new parent object;
if I'm not mistaken, tmp2 is going to be of type parent * and a copy
of tmp.
 
Try this:
 
1 #include<iostream>
2
3 class parent{
4 public:
5 parent():_something{nullptr}, _val{0} {
6 std::cout << "null constructor\n";
7 };
8 parent(parent *in):_something{in}, _val{10} {
9 std::cout << "here\n";
10 _something->_val = 0;
11 };
12 parent * _something;
13 int _val;
14 };
15 void discover(parent * in){
16 std::cout<< in->_val << std::endl;
17 std::cout<< in->_something->_val << std::endl;
18 }
19
20 int main()
21 {
22 parent * tmp = new parent;
23 parent tmp2{tmp};
24 //parent * tmp2 = new parent(tmp);
25 discover(&tmp2);
26
27 return 0;
28 }
 
Louis
ruben safir <ruben@mrbrklyn.com>: Nov 20 11:33PM -0500

On 11/20/2016 11:06 PM, Louis Krupp wrote:
> 27 return 0;
> 28 }
 
> Louis
 
Right but there seems to be no way, without calling new, to create an
anonymous object which is pointed to by parent * myvar
Juha Nieminen <nospam@thanks.invalid>: Nov 21 07:58AM


> Because "parent * tmp2{tmp}" isn't constructing a new parent object;
> if I'm not mistaken, tmp2 is going to be of type parent * and a copy
> of tmp.
 
If you are not mistaken? Sheesh.
 
'tmp' is a pointer to something. 'tmp2' is another pointer of the same type.
It's assigned the pointer 'tmp'. Thus it will just point to the same object.
Only one instance of 'parent' is being created here (using 'new').
Paavo Helde <myfirstname@osa.pri.ee>: Nov 21 10:10AM +0200

On 21.11.2016 6:33, ruben safir wrote:
 
>> Louis
 
> Right but there seems to be no way, without calling new, to create an
> anonymous object which is pointed to by parent * myvar
 
You are about right, if I understand correctly what you mean by
"anonymous", and if you want the object to stay alive for longer term.
 
This is because in C++ the object lifetimes are deterministic. Either
you create the object on stack (automatic storage duration) and then it
needs a named object or at least a const reference to keep it alive, or
alternatively, you create an object via new or equivalent (dynamic
storage duration) and then you need a pointer for accessing and finally
destroying the object.
 
The nearest you can get to "a non-newed object and pointer" is "a
non-newed object and a const reference". There is a special rule that a
const reference keeps a temporary object alive during the lifetime of
the reference.
 
 
#include<iostream>
 
class parent{
public:
parent():_something{nullptr}, _val{0} {
std::cout << "null constructor\n";
};
parent(parent *in):_something{in}, _val{10} {
std::cout << "here\n";
_something->_val = 0;
};
parent * _something;
int _val;
};
void discover(const parent * in){
std::cout<< in->_val << std::endl;
std::cout<< in->_something->_val << std::endl;
}
 
 
int main()
{
parent * tmp = new parent;
const parent& tmp2 = parent {tmp};
discover(&tmp2);
 
return 0;
}
ruben safir <ruben@mrbrklyn.com>: Nov 21 04:51AM -0500

On 11/21/2016 02:58 AM, Juha Nieminen wrote:
> If you are not mistaken? Sheesh.
 
why bother typing after that? Nothing else is read. I know that you
think that one who is asking for information will certainly read past
this but frankly, in my old age, I don't feel the need.
 
/dev/null
 
there is more to life than the right answer for a programming language.
I can put the keyboard down and play with my grandchildren.
 
I hope I've made my point without being unnecessarily rude.
 
Reuvain
ruben safir <ruben@mrbrklyn.com>: Nov 21 04:55AM -0500

On 11/21/2016 03:10 AM, Paavo Helde wrote:
 
thank you. You've clear up what I find is a subtle point which can
cause great bugs. I appreciate you taking the time to discuss this with me.
 
Reuvain
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 21 02:59PM +0100

On 21.11.2016 10:51, ruben safir wrote:
 
> why bother typing after that? Nothing else is read. I know that you
> think that one who is asking for information will certainly read past
> this but frankly, in my old age, I don't feel the need.
 
You were introducing needless and misleading uncertainty.
 
 
 
> there is more to life than the right answer for a programming language.
> I can put the keyboard down and play with my grandchildren.
 
> I hope I've made my point without being unnecessarily rude.
 
Juha was entirely correct in pointing out that the uncertainty you
expressed, about fundamental stuff, was unwarranted and misleading.
 
As he wrote, "Sheesh".
 
 
Cheers & hth.,
 
- Alf ;-)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 21 03:01PM +0100

s/you/Louis/g
 
@Ruben: Your own fault for removing all context, heh.
 
 
On 21.11.2016 14:59, Alf P. Steinbach wrote:
ram@zedat.fu-berlin.de (Stefan Ram): Nov 21 09:01AM

I would like to show an example of a simple static data member
in a standard-library class. It should /not/ be a template class.
 
I came up with this:
 
#include <initializer_list>
#include <iostream>
#include <memory_resource>
#include <ostream>
 
int main()
{ ::std::cout << ::std::memory_resource::max_align << '\n'; }
 
But it seems that my compiler does not know this header
from 20.12.
 
Are you aware of any other example I could use that is
less far-flung?
ram@zedat.fu-berlin.de (Stefan Ram): Nov 21 09:20AM

>Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>Is nullptr not a pointer?
>std::nullptr_t is a distinct type that is neither a pointer
 
main.cpp
 
#include <iostream>
#include <ostream>
#include <type_traits>
 
template< typename T > void describe( T const )
{ ::std::cout << ::std::is_pointer< T >::value << '\n'; }
 
int main()
{ describe( nullptr ); }
 
transcript
 
0
Juha Nieminen <nospam@thanks.invalid>: Nov 21 07:46AM

> why does it need a .h? Why does it not work like #include <cstdlib>
 
The compiler has a file named "cstdlib". It does not have a file named "zlib"
or "czlib". If there exists no file with that name, it obviously can't include it.
Juha Nieminen <nospam@thanks.invalid>: Nov 21 07:47AM

> that also goes for including the .h which we, by convention do in C but
> in C++ we drop the .h from the #includes?
 
The .h was dropped from the standard header *file names*. There is
literally a file named "cstdlib" in your compiler's standard library
directory.
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: