Friday, July 17, 2015

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

Lynn McGuire <lmc@winsim.com>: Jul 17 12:59PM -0500

"The power of C++11 in creating Windows smart pointers"
http://www.codeproject.com/Articles/1010168/The-power-of-Cplusplus-in-creating-Windows-smart-p
 
Lynn
Doug Mika <dougmmika@gmail.com>: Jul 17 09:56AM -0700

Hi
 
I'm using the online version of a C++11 IDE at tutorialspoint. It however is constantly down and unavailable for the last 3 days at least. Does anyone know of another good online C++11 IDE that supports threads?
 
Thanks
Doug
"Öö Tiib" <ootiib@hot.ee>: Jul 17 10:31AM -0700

On Friday, 17 July 2015 19:56:54 UTC+3, Doug Mika wrote:
> I'm using the online version of a C++11 IDE at tutorialspoint. It however is
> constantly down and unavailable for the last 3 days at least. Does anyone
> know of another good online C++11 IDE that supports threads?
 
Can you elaborate why you do that?
Why not to put your code projects into online repos (like github) and to
use local IDE?
Every IDE I've ever touched (NetBeans, QtCreator, Eclipse, Visual Studio,
XCode etc.) are far better than those online IDEs.
Doug Mika <dougmmika@gmail.com>: Jul 17 10:53AM -0700

On Friday, July 17, 2015 at 12:32:15 PM UTC-5, Öö Tiib wrote:
> use local IDE?
> Every IDE I've ever touched (NetBeans, QtCreator, Eclipse, Visual Studio,
> XCode etc.) are far better than those online IDEs.
 
I have NetBeans with MinGW installed, and I have not been able to figure out how to get threads to work on it...for a while now. Tutorialspoint IDE allowed for threading with merely the -pthreads tag.
Doug Mika <dougmmika@gmail.com>: Jul 17 09:52AM -0700

Hi to all
 
I have been reading about C++ for a while, but I must admit, that one of the things that I find confusing is all the instances when you use the scope resolution operator ::. Namespaces, inheritence, etc...
 
So it begs the question, when we write vector<int>::const_iterator myIterator
can I deduce from this that const_iterator is an inner class of vector<int> class? Is it?
 
Does anyone know of a easy to read and comprehensive introduction to how/when/why to use the scope resolution operator?
red floyd <no.spam@its.invalid>: Jul 17 10:08AM -0700

On 7/17/2015 9:52 AM, Doug Mika wrote:
 
> I have been reading about C++ for a while, but I must admit, that one of the things that I find confusing is all the instances when you use the scope resolution operator ::. Namespaces, inheritence, etc...
 
> So it begs the question, when we write vector<int>::const_iterator myIterator
> can I deduce from this that const_iterator is an inner class of vector<int> class? Is it?
 
No. It is a *typename* that is defined within vector<int>. It is not
necessarily an inner class.
 
Example:
 
class A {
public:
typedef int integral_type;
};
 
A::integral_type x = 0;
 
x is of type int.
Doug Mika <dougmmika@gmail.com>: Jul 16 07:02PM -0700

Why is the following a compile time error and not a bad_cast exception:
 
// bad_cast example
#include <iostream> // std::cout
#include <typeinfo> // std::bad_cast
 
class Base {virtual void member(){}};
class Derived : Base {};
 
int main () {
try
{
Base b;
Derived rd = dynamic_cast<Derived>(b);
}
catch (std::bad_cast& bc)
{
std::cerr << "bad_cast caught: " << bc.what() << '\n';
}
return 0;
}
 
I should mention that it IS a bad_cast exception if I include ampersands (&) in
 
Derived& rd = dynamic_cast<Derived&>(b);
 
but why?
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 01:56AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> Why is the following a compile time error and not a bad_cast
> exception:
 
Because dynamic_cast is defined to work only on pointers and references.
 
> ampersands (&) in
 
> Derived& rd = dynamic_cast<Derived&>(b);
 
> but why?
 
Because dynamic_cast is defined to work only on pointers and references.
"Öö Tiib" <ootiib@hot.ee>: Jul 16 11:59PM -0700

On Friday, 17 July 2015 05:03:00 UTC+3, Doug Mika wrote:
 
> I should mention that it IS a bad_cast exception if I include ampersands (&) in
 
> Derived& rd = dynamic_cast<Derived&>(b);
 
> but why?
 
Because of syntax error that is diagnosed compile time.
There in 'dynamic_cast<T>(x)' that 'T' is required to be pointer to
complete type, reference to complete type or pointer to void and
'Derived' is neither of those so compiler can't parse what you want.
Doug Mika <dougmmika@gmail.com>: Jul 17 08:45AM -0700

On Thursday, July 16, 2015 at 9:03:00 PM UTC-5, Doug Mika wrote:
 
> I should mention that it IS a bad_cast exception if I include ampersands (&) in
 
> Derived& rd = dynamic_cast<Derived&>(b);
 
> but why?
 
So in the example that compiles, with ampersands, (I include the example again below) why doesn't it simply return rd=nullptr, why does it throw an exception. My question I guess is, when does dynamic cast throw an exception and when does it return nullptr?
 
// bad_cast example
#include <iostream> // std::cout
#include <typeinfo> // std::bad_cast
 
class Base {virtual void member(){}};
class Derived : Base {};
 
int main () {
try
{
Base b;
Derived& rd = dynamic_cast<Derived&>(b);
}
catch (std::bad_cast& bc)
{
std::cerr << "bad_cast caught: " << bc.what() << '\n';
}
return 0;
}
"K. Frank" <kfrank29.c@gmail.com>: Jul 17 09:08AM -0700

Hi Doug!
 
See my comment, inline below:
 
On Friday, July 17, 2015 at 11:45:32 AM UTC-4, Doug Mika wrote:
> > Why is the following a compile time error and not a bad_cast exception:
> > ...
 
> So in the example that compiles, with ampersands, (I include the example again below) why doesn't it simply return rd=nullptr, why does it throw an exception.
 
Because you are casting to a reference, not a pointer,
so it can't return a nullptr.
 
> My question I guess is, when does dynamic cast throw an exception and when does it return nullptr?
 
The short answer is because that's how dynamic_cast is
designed to work. But the reason is that references can't
be nullptrs so there is no "singular value" for the reference
version of dynamic_cast to return to signal the "bad cast"
error condition. Hence it is designed to throw an exception.
 
> {
> Base b;
> Derived& rd = dynamic_cast<Derived&>(b);
 
rd is a reference, so can't be a nullptr. The
dynamic_cast<SomeType&> returns a reference type,
and can't return a nullptr (because nullptr is
a pointer type, not a reference type).
 
> }
 
...
 
 
Good luck.
 
 
K. Frank
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 11:09AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> So in the example that compiles, with ampersands, (I include the
> example again below) why doesn't it simply return rd=nullptr, why does
> it throw an exception.
 
A reference cannot have nullptr value.
 
> My question I guess is, when does dynamic cast
> throw an exception and when does it return nullptr?
 
It returns nullptr if this is possible, otherwise throws.
Doug Mika <dougmmika@gmail.com>: Jul 16 05:55PM -0700

Why doesn't the following program throw a bad_cast exception? How can the cast possibly suceed?
 
#include <iostream>
#include <typeinfo>
 
using namespace std;
 
class Mammel{
public:
void Run(){
cout<<"Mammel runs on land"<<endl;
}

virtual ~Mammel(){}
};
 
class Human:public Mammel{
public:
void Run(){
cout<<"Human runs slow"<<endl;
}
};
 
class Fish{
public:
void Swim(){
cout<<"Fish swims in water\n";
}

virtual ~Fish(){}

};
 
class Tuna:public Fish{
public:
void Swim(){
cout<<"Tuna swims really fast\n";
}
};
 
class Carp:public Fish{
public:
void Swim(){
cout<<"Carp swims really slow\n";
}
};
 
 
int main()
{
Human* Charles = new Human();
Carp* carp;

try{
carp = dynamic_cast<Carp*>(Charles);
cout<<"Cast succeeded"<<endl;
}catch(std::bad_cast& exc){
cout<<"Exception: Bad Cast"<<exc.what()<<endl;
}

return 0;
}
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 01:50AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> Why doesn't the following program throw a bad_cast exception? How can
> the cast possibly suceed?
 
It does not succeed - the resulting pointer will be NULL. The bad_cast
exception is thrown only when casting to a reference type. By using
either a pointer or a reference type you decide whether you want an
automatic exception or not.
 
Doug Mika <dougmmika@gmail.com>: Jul 17 08:33AM -0700

On Thursday, July 16, 2015 at 7:56:14 PM UTC-5, Doug Mika wrote:
> }
 
> return 0;
> }
 
What do you mean by an automatic exception?
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 11:06AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> What do you mean by an automatic exception?
 
bad_cast
fl <rxjwg98@gmail.com>: Jul 17 08:30AM -0700

Hi,
 
I find the following code snippet on line. I feel that is interesting for my
learning.
 
This part is difficult to understand for me.
 
cBase*& operator[](unsigned index)
{
// much more complicated, but simplified for example
return array[index];
}
 
Could you explain it to me?
'*&' looks weird I feel.
 
Thanks,
 
 
 
.......
// the classes
class cBase {};
class cDerived : public cBase {};
 
class cBaseArray
{
// the array of pointers to cBase
cBase** array;
 
// overloaded operator that returns an element of the array
cBase*& operator[](unsigned index)
{
// much more complicated, but simplified for example
return array[index];
}
};
Bo Persson <bop@gmb.dk>: Jul 17 05:34PM +0200

On 2015-07-17 17:30, fl wrote:
> }
 
> Could you explain it to me?
> '*&' looks weird I feel.
 
It's just a reference, nothing odd.
 
Below you have Base** which is a pointer to a pointer to Base.
Similarly, Base*& is a reference to a pointer to Base.
 
 
Paul <pepstein5@gmail.com>: Jul 17 06:00AM -0700

Some have said on other threads to use raw pointers instead of unique_ptr when iterating in a container. However, other advice has been just the opposite. For example http://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers and http://marknelson.us/2012/06/24/c11-unique_ptrt/
 
Suppose, for example, that I'm designing a tree where the number of nodes descending from any particular node is variable. Any reason not to put nodes in vectors of std::unique_ptr<T>
 
Thank you,
 
Paul
bartekltg <bartekltg@gmail.com>: Jul 17 05:10PM +0200

On 17.07.2015 15:00, Paul wrote:
> Some have said on other threads to use raw pointers instead of
> unique_ptr when iterating in a container.
 
" iterating in a container."
I don't understand.
 
It looks like all 'gurus' from c++ say to use unique pointers.
 
It is all about keeping RAII in dynamic alocated objects. About
who own what. If you container of pointers is a main owner,
of objects, no other long living object 'borrow it',
unique_ptr is a good idea.
 
> However, other advice has been just the opposite. For example
>http://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers
> and http://marknelson.us/2012/06/24/c11-unique_ptrt/
 
There:
https://www.youtube.com/user/CppCon
somewhere is quite good lecture about poiters and optimalizations,
and speaker said if you have function that not relocate ownership,
raw pointers are best solution. Chain of function have raw pointer
as argument, and the top function is call with unique_ptr
as an argument.
 
There is something about this topic
https://www.youtube.com/watch?v=xnqTKD8uD64
 
https://github.com/CppCon/CppCon2014/blob/master/Presentations/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style%20-%20Herb%20Sutter%20-%20CppCon%202014.pdf
 
And lecture is very interesting, but I'm sure there was one more focused
on pointers. Sorry, I don't remember which one.
 
 
> Suppose, for example, that I'm designing a tree where the number of
> nodes descending from any particular node is variable. Any reason
> not to put nodes in vectors of std::unique_ptr<T>
 
If you don't keep pointers (raw in case of uniqueptr) in other places
I think it is nice idea.
 
In a node you erase one node (pointing at children) from vector. This
is unique_ptr, so it runs destructor of targeted node. And node have a
vector of unique ptr, so destructor of vestor and all pointers is
called...
You erased one node, all children (recursively) are erased and its
memory freed automatically.
One function less to write ;-)
 
 
But it works only because it is a tree.
 
bartekltg
"Öö Tiib" <ootiib@hot.ee>: Jul 17 08:11AM -0700

On Friday, 17 July 2015 16:00:52 UTC+3, Paul wrote:
> Some have said on other threads to use raw pointers instead of unique_ptr
> when iterating in a container.
 
No. Write special wrapper classes around raw pointers that
are called iterator for iterating/navigating in containers.
 
You can alternatively use the raw pointers directly as poor
man's iterators but that exposes all the details of navigation.
 
There are no way how to use smart pointers for navigating
because all what smart pointers are doing is managing
resources and that is exactly something that someone who
is navigating around in those managed resources does not
want to do.
 
> However, other advice has been just the opposite.
> For example http://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers
 
Read it again. It is suggesting to use 'unique_ptr' as the
data that is *stored* in standard containers nowhere as
iterators that are *navigating* in containers.
 
and http://marknelson.us/2012/06/24/c11-unique_ptrt/
 
Same there.
 
> Suppose, for example, that I'm designing a tree where the
> number of nodes descending from any particular node is variable.
> Any reason not to put nodes in vectors of std::unique_ptr<T>
 
You can put C++11 smart pointers into all standard containers
('std::auto_ptr' was suitable for only some). You can use the
smart pointers for building your own containers (but you have
to avoid circular references). However you can't use smart pointer
as iterator of your container.
bartekltg <bartekltg@gmail.com>: Jul 17 05:06PM +0200

On 16.07.2015 20:16, Öö Tiib wrote:
>> big integer from GMP. What do you like and need. It doesn't matter.
 
> Factorial algorithms for arbitrary precision integer are different beast
> entirely. Those can take some time.
 
I have never saw one.
 
 
 
> output "Bézout coefficients:", (old_s, old_t)
> output "greatest common divisor:", old_r
> output "quotients by the gcd:", (t, s)
 
Heh, dementia;-] I was sure wiki have recursive version,
what other source could I have used to write that (as an
argument for usefulness of tuple) ;-)
 
tuple<int, int, int> egcd(int a,int b)
{ //return {gcd, x, y}; a*x+b*y = gcd;
 
if (b==0)
return make_tuple(a,1,0);
else
{
int g,x,y;
tie(g,y,x) = egcd(b,a%b);
return make_tuple(g,x, y - ((a / b)) * x);
}
}
 
Getting the first version from the second isn't as hard a I thought,
but still not trivial.
 
 
>> I think of course you can, but it won't be pretty
>> and wou would waste too much time.
 
> Might be you mix something up here ... it is simple algorithm.
 
No, its my fault, I didn't check and in my small math 'forfun'
library I used recursion.
 
> Calculating 10,000,000! naively will take minutes and there are
> ways to optimize that:
> http://www.cs.berkeley.edu/~fateman/papers/factorial.pdf
 
Interesting sport. I like it ;-)
 
 
> Sorry it is lisp, but the major number crunchers tend to love lisp.
 
)))))))))))))))
 
If you work with number theory you have to have enough
"RAM" in brain to track all brackets without effort :)
 
bartekltg
Victor Bazarov <v.bazarov@comcast.invalid>: Jul 17 07:53AM -0400

On 7/16/2015 5:31 PM, fl wrote:
 
> First of all, I am not sure about whether there is a conversion to a reference
> or not from the above. (There is a reference conversion, though not change
> the derived-type object in any way? This sentence is puzzling me a lot.)
 
A reference to the base class is bound to the area in the object (of the
derived type) that constitutes the base class *subobject*. If you think
of a reference as a kind of a pointer, that pointer points to the
address of the subobject inside that [unchanged] derived class object.
 
> from a derived object:"
 
> This paragraph clearly said there is a reference conversion from derived to
> the base. Could you make it clear to me?
 
Derived class object layout:
[ ... [base class object] ... other base classes and data ]
^
+--- the reference to the derived class object refers to the whole
 
After "conversion" the reference to the base class object is set to
refer to the portion of the object
[ ... [base class object] ... other base classes and data ]
. ^
. +--- the reference to the base class object refers here
 
To the code the portion of the [unchanged] *derived* class object is not
"visible". When you manipulate the object using the reference to the
*base* class (object), it only affects the area that represents the base
class object (here inside the derived class object).
 
I am not sure I made it clearer, so ask more questions if necessary.
 
V
--
I do not respond to top-posted replies, please don't ask
fl <rxjwg98@gmail.com>: Jul 17 05:58AM -0700

On Friday, July 17, 2015 at 4:54:24 AM UTC-7, Victor Bazarov wrote:
 
> V
> --
> I do not respond to top-posted replies, please don't ask
 
Your reply makes it very clear to me now! Great thanks.
Ralf Goertz <me@myprovider.invalid>: Jul 17 07:57AM +0200

Am Thu, 16 Jul 2015 08:27:58 -0700 (PDT)
 
> [boost.range program]
 
I like that one.
 
> Feels cheaper than to find out 'distance' from
> start of 'set' for each iteration and safer than with
> separate counter.
 
Definitely. Thanks for the suggestion. This shows that boost still has a
lot of stuff worth discovering.
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: