Friday, July 17, 2015

Digest for comp.lang.c++@googlegroups.com - 11 updates in 5 topics

"Öö Tiib" <ootiib@hot.ee>: Jul 17 11:25AM -0700

On Friday, 17 July 2015 20:54:00 UTC+3, Doug Mika wrote:
> > 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.
 
Not sure what the trick is with you. If to select pthread from the mingw-w64
installer then threads work out of the box.
 
Hmm ... perhaps it is because you use win32? Last 6 years it has been quite
unlikely to buy new computer that is incapable of running 64 bit operating
system.
 
If you for whatever reason use mingw-w32 then perhaps you need to take
std::thread and sync primitives implementation for it from somewhere.
That looks good: https://github.com/meganz/mingw-std-threads
Doug Mika <dougmmika@gmail.com>: Jul 17 12:38PM -0700

On Friday, July 17, 2015 at 11:56:54 AM UTC-5, 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?
 
> Thanks
> Doug
 
You mean, when installing mingw-w64 there is an option during installation to select pthread, which allows threads to work?
"K. Frank" <kfrank29.c@gmail.com>: Jul 17 01:31PM -0700

Hello Doug (and Öö)!
 
On Friday, July 17, 2015 at 2:26:00 PM UTC-4, Öö Tiib wrote:
 
> If you for whatever reason use mingw-w32 then perhaps you need to take
> std::thread and sync primitives implementation for it from somewhere.
> That looks good: https://github.com/meganz/mingw-std-threads
 
(As an aside, the last time I looked at mingw-std-threads, it
wasn't really there yet. But that was at least half a year ago,
if not more.)
 
Just to clarify, mingw and mingw-w64 are two separate projects,
the latter a fork of the former, and they have two separate
mailing lists.
 
mingw does not support (non-native-windows) threads out of the
box, and does not support std::thread.
 
mingw-w64 does offer versions that support both pthreads (posix
c-style threads) and std::thread (c++11 threads) out of the box.
(But you will need to tell g++ to turn on c++11 features to
get std::thread, e.g., "-std=c++11" or "-std=c++14" or similar.)
 
You can run the mingw version with pthreads-win32 (a separate
download), to get pthreads, but this won't get you std::thread.
 
I have been using mingw-w64 with std::thread for some time now.
(I don't really use pthreads.)
 
(Also, mingw offers only a 32-bit version -- runs as a 32-bit
application and produces 32-bit code -- while mingw-w64 offers
both 32-bit and 64-bit versions. mingw will run, of course, on
64-bit windows as a 32-bit application. It will produce 32-bit
applications that will run on 64-bit windows. But if you need
to produce 64-bit applications, you will need mingw-w64.)
 
If you have further questions about either of these two ports
of gcc to windows, you are likely to get better-informed
responses on their respective mailing lists.
 
 
Happy Multi-Threaded hacking!
 
 
K. Frank
Ian Collins <ian-news@hotmail.com>: Jul 18 11:20AM +1200

Doug Mika wrote:
 
[Please wrap your lines!]
 
> 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?
 
Don't bother! If you are using windows and want to minimise the pain,
use the community edition of Visual Studio.
 
--
Ian Collins
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 17 11:18PM +0200

On 16-Jul-15 10:39 AM, Ralf Goertz wrote:
> }
> return 0;
> }
 
In Python there is a built-in function `enumerated` that produces a
corresponding sequence of (index, value) pairs:
 
raw_array = [1.2, 3.4, 5.6]
for x in enumerate( raw_array ):
print( "%d -> %g" % (x[0], x[1]) )
 
How to do that in C++?
 
Well, Öö Tiib has already mentioned the Boost Range library's `indexed`,
and I would guess the Python `enumerate` facility served as inspiration
for that.
 
If you don't want a dependency on Boost, or if you just don't want the
over-engineered custom operator| thing, then it's not hard to do this
yourself, e.g. like the following -- I wrote this code in response to
your posting, so it's not been tested more than the little program:
 
<file "Indexer.hpp">
#pragma once
// Copyright (c) 2015 Alf P. Steinbach
 
#include <iterator> // std::iterator_traits
#include <stddef.h> // ptrdiff_t
#include <type_traits> // std::remove_reference
#include <utility> // std::pair, std::declval, std::begin,
std::end, std::move
 
namespace progrock{ namespace cppx{
using std::begin;
using std::declval;
using std::end;
using std::iterator;
using std::iterator_traits;
using std::move;
using std::pair;
using std::random_access_iterator_tag;
using std::remove_reference;
 
using Index = ptrdiff_t;
 
template< class Underlying_iter_tp, class Index_tp = Index >
class Indexer
{
public:
using Underlying_iter = Underlying_iter_tp;
using Index = Index_tp;
 
using Item = typename remove_reference<decltype((
*declval<Underlying_iter>() ))>::type;
 
struct Numbered_item
: pair<Index, Item*>
{
using pair<Index, Item*>::pair;
 
auto index() const -> Index { return first; }
auto value() -> Item& { return *second; }
auto value() const -> Item const& { return *second; }
};
 
private:
Underlying_iter under_begin_;
Underlying_iter under_end_;
 
class Iter
: public iterator<
typename
iterator_traits<Underlying_iter>::iterator_category,
Numbered_item,
typename iterator_traits<Underlying_iter>::difference_type,
typename iterator_traits<Underlying_iter>::pointer,
typename iterator_traits<Underlying_iter>::reference
 
{
friend class Indexer;
private:
Underlying_iter it_;
Numbered_item item_;
 
Iter( Underlying_iter it )
: it_( move( it ) )
, item_( 0, &*it ) // TODO: Check validity
{}
 
public:
friend
auto operator<( Iter const& a, Iter const& b )
-> bool
{ return (a.it_ < b.it_); }
 
friend
auto operator<=( Iter const& a, Iter const& b )
-> bool
{ return (a.it_ <= b.it_); }
 
friend
auto operator==( Iter const& a, Iter const& b )
-> bool
{ return (a.it_ == b.it_); }
 
friend
auto operator>=( Iter const& a, Iter const& b )
-> bool
{ return (a.it_ >= b.it_); }
 
friend
auto operator>( Iter const& a, Iter const& b )
-> bool
{ return (a.it_ > b.it_); }
 
friend
auto operator!=( Iter const& a, Iter const& b )
-> bool
{ return (a.it_ != b.it_); }
 
auto operator++()
-> Iter&
{
++it_;
++item_.first; item_.second = &*it_;
return *this;
}
 
auto operator++( int )
-> Iter
{
Iter original = *this;
++*this;
return original;
}
 
auto operator*()
-> Numbered_item const&
{ return item_; }
};
 
public:
auto begin() -> Iter { return Iter( under_begin_ ); }
auto end() -> Iter { return Iter( under_end_ ); }
 
Indexer( Underlying_iter const begin_iter, Underlying_iter
const end_iter )
: under_begin_( begin_iter )
, under_end_( end_iter )
{}
};
 
template< class Collection_tp >
auto indexed( Collection_tp& c )
-> Indexer<decltype( begin( c ) )>
{ return Indexer<decltype( begin( c ) )>( begin( c ), end( c ) ); }
}} // namespace progrock::cppx
</file>
 
<file "main.cpp">
#include "Indexer.hpp"
using namespace progrock;
using cppx::indexed;
 
#include <iostream>
#include <array>
#include <list>
#include <vector>
using namespace std;
 
template< class Collection >
void display( Collection const& c )
{
for( auto const item : indexed( c ) )
{
wcout << item.index() << L" → " << item.value() << endl;
}
}
 
auto main() -> int
{
display( array<double, 3>{1.2, 3.4, 5.6} );
wcout << endl;
display( vector<double>{1.2, 3.4, 5.6} );
wcout << endl;
display( list<double>{1.2, 3.4, 5.6} );
wcout << endl;
 
vector<double> v(5);
for( auto item : indexed( v ) )
{
item.value() = 0.1*item.index();
}
display( v );
}
</file>
 
The little arrow in the output may be displayed incorrectly or may cause
truncation of the output. One fix is to replace it with "->", which has
only ASCII characters. Another fix is to configure the C++ i/o properly,
which, sadly, is not done by default by current implementations; for
code that does this see e.g.
 
<url:
http://stackoverflow.com/questions/30197758/how-can-i-make-unicode-iostream-i-o-work-in-both-windows-and-unix-land>
 
And if you want to display that little arrow with Python in Windows
consoles, see e.g.
 
<url:
https://alfps.wordpress.com/2015/05/12/non-crashing-python-3-x-output-in-windows/>
 
 
Cheers & hth.,
 
- Alf
 
--
Using Thunderbird as Usenet client, Eternal September as NNTP server.
Doug Mika <dougmmika@gmail.com>: Jul 17 11:10AM -0700

On Friday, July 17, 2015 at 11:52:39 AM UTC-5, Doug Mika wrote:
 
> 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?
 
So what precisely is the line inside vector<T>
 
typedef ? const_iterator;
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jul 17 06:51PM

On Fri, 2015-07-17, Doug Mika wrote:
> 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?
 
*Shrug* Call me lazy, but I use :: when I have to; when the compiler
refuses to understand me if I don't.
 
For vector<int>::const_iterator it's obvious that I have to; there is
no all-purpose "const_iterator", and I'm not inside the implementation
of vector<int>, so I have to be specific (or use "auto").
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 02:16PM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> So what precisely is the line inside vector<T>
 
> typedef ? const_iterator;
 
http://www.learncpp.com/cpp-tutorial/46-typedefs/
"Öö Tiib" <ootiib@hot.ee>: Jul 17 12:27PM -0700

On Friday, 17 July 2015 21:10:20 UTC+3, Doug Mika wrote:
 
> > Does anyone know of a easy to read and comprehensive introduction to how/when/why to use the scope resolution operator?
 
> So what precisely is the line inside vector<T>
 
> typedef ? const_iterator;
 
It is required to be a type that fulfills requirements to random access
iterator to const T.
http://www.cplusplus.com/reference/iterator/RandomAccessIterator/
 
What it really is may depend on compiler options. Compiler's
documentation may specify more about it but usage of such
implementation-specific knowledge results with non-portable code.
Barry Schwarz <schwarzb@dqel.com>: Jul 17 11:31AM -0700

On Fri, 17 Jul 2015 08:30:13 -0700 (PDT), fl <rxjwg98@gmail.com>
wrote:
 
>learning.
 
>This part is difficult to understand for me.
 
> cBase*& operator[](unsigned index)
 
Does it make sense to you if the & were not there. The function would
return a value. That value would have type "pointer to cBase." By
way of example, strcpy also returns a value. That value has type
pointer to char.
 
Now put the & back in. Now the function returns a reference. That
reference "refers" to an object of type "pointer to cBase".
 
> {
> // much more complicated, but simplified for example
> return array[index];
 
Obviously, the reference being returned refers to the object
array[index].
 
>{
> // the array of pointers to cBase
> cBase** array;
 
array has type cBase**. This means that cBase points to an object of
type cBase*. From the context, it appears obvious the object cBase
points to is followed in memory by several more objects of the same
type. Normal array notation allows array[0] to designate the first of
these objects, array[1] the second, etc. array[index] designates one
of these objects, which one being determined at run time.
 
> {
> // much more complicated, but simplified for example
> return array[index];
 
Since all the objects pointed to by array have type cBase*, object
array[index] has this type. The reference that is returned designates
this object. This allows the calling function to use this reference
to access the cBase object array[index] points to. It also allows the
calling function to change the value in array[index] itself so that it
points to some other object of type cBase.
 
If the & were missing, the calling function would receive the value of
array[index] and could access the cBase object pointed to but could
not change the value of array[index] itself.
 
Whether the calling function should be allowed to change a value in
one of the array objects is a different question.
 
 
--
Remove del for email
doctor@doctor.nl2k.ab.ca (The Doctor): Jul 16 11:54PM

In article <d0qlnkFmch1U3@mid.individual.net>,
>than here.
 
>--
>Ian Collins
 
Google this for fun. Even the MySSQL Community development team
gets flamed for thei inconsistency.
 
I look on Search Engines frist before coming here.
 
--
Member - Liberal International This is doctor@@nl2k.ab.ca Ici doctor@@nl2k.ab.ca
God,Queen and country!Never Satan President Republic!Beware AntiChrist rising!
http://www.fullyfollow.me/rootnl2k Look at Psalms 14 and 53 on Atheism
Abuse a man unjustly, and you will make friends for him. -Edgar Watson Howe
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: