Tuesday, April 14, 2015

Digest for comp.lang.c++@googlegroups.com - 12 updates in 8 topics

Nikki Locke <nikki@trumphurst.com>: Apr 14 10:22PM

Available C++ Libraries FAQ
 
URL: http://www.trumphurst.com/cpplibs/
 
This is a searchable list of libraries and utilities (both free
and commercial) available to C++ programmers.
 
If you know of a library which is not in the list, why not fill
in the form at http://www.trumphurst.com/cpplibs/cppsub.php
 
Maintainer: Nikki Locke - if you wish to contact me, please use the form on the website.
boris.yablonsky@gmail.com: Apr 14 02:03PM -0700

I wanted to share with the community that the book "Cracking Programming Interviews: 500 Questions with Solutions" is an awesome book with great compilation of algorithmic questions with great solutions, that to in C++11/C++14:
 
I enjoyed solving the problems :
 
Select Kth Smallest Element, Searching two-dimensional
sorted array, 2D Array Rotation, Interpolation Search, Finding the miss-
ing number with sorted columns, Lowest Common Ancestor(LCA) Problem,
String Edit Distance, Max Sub-Array Problem, Matching Nuts and Bolts Opti-
mally, Spying Campaign, Find Median of two sorted arrays, Finding Ranks in
Linked Lists, Linear Time Sorting, The problem of the most isolated villages,
The Celebrity Problem, Sorting an almost sorted list, Generating Permutation
Efficiently, Bit Reversal, Bit-wise Rotation, Parallel Addition and many more.
 
http://www.amazon.com/Cracking-Programming-Interviews-Questions-Solutions/dp/1495459802/
 
It greatly supplements Cormen book on algorithms.
lin.quan.20@gmail.com: Apr 14 01:57PM -0700

I am trying to understand C++14 from these books :
 
Effective Modern C++ by Scott Meyers, and
C++14 FAQs by Chandra Shekhar Kumar
 
So far, I find the later book to a better one for C++14 whereas the first book is a generic one. But I do not see much of the reviews of the book C++14 FAQs on Amazon yet whereas the first book is highly rated, may be over rated ??
Doug Mika <dougmmika@gmail.com>: Apr 14 12:50PM -0700

Hi
 
I supposedly have MinGW version 4.8.1 (gcc --version yields that version) and I'm using NetBeans as an IDE. However, when I try to compile my code, the following line poses an issue:
 
auto iElement = find_if(vecIntegers.begin(), vecIntegers.end(), myObj);
 
and the complaint is that iElement does not name a type. When I replace the line with
 
std::vector<int>::const_iterato iElement = ...;
 
the program compiles.
 
BUT according to http://gcc.gnu.org/gcc-4.8/cxx0x_status.html, MinGW 4.8.x is supposedly supposed to support the auto typed variables
 
Anyone have any idea why auto doesn't seem to work?
Doug Mika <dougmmika@gmail.com>: Apr 14 01:22PM -0700

On Tuesday, April 14, 2015 at 2:50:44 PM UTC-5, Doug Mika wrote:
 
> the program compiles.
 
> BUT according to http://gcc.gnu.org/gcc-4.8/cxx0x_status.html, MinGW 4.8.x is supposedly supposed to support the auto typed variables
 
> Anyone have any idea why auto doesn't seem to work?
 
Thanks, that worked.
legalize+jeeves@mail.xmission.com (Richard): Apr 14 08:14PM

[Please do not mail me a copy of your followup]
 
Doug Mika <dougmmika@gmail.com> spake the secret code
>I know that overloading an operator< allows for the direct comparison of
>my two objects using <, but what about the operator(), especially in a
>struct? Does it allow the struct to be called like a function?
 
The standard library often has algorithms that take predicates as
arguments. For instance, std::list::remove_if takes a UnaryPredicate
argument <http://en.cppreference.com/w/cpp/container/list/remove>:
 
"template <class UnaryPredicate>
void remove_if(UnaryPredicate p);
 
Removes all elements satisfying specific criteria [...]
removes all elements for which predicate p returns true."
 
The argument p is a model of UnaryPredicate, a function that can be
called like:
 
bool pred(int a);
 
for a std::list<int>. You can supply a predicate function in many
ways, such as:
 
1) a pointer to a function
 
bool is_odd(int a) { return (a % 2) == 1; }
// ...
mylist.remove_if(is_odd);
 
2) a lambda function
 
my_list.remove_if([](int a) { return (a % 2) == 1; });
 
3) a function object
 
struct is_odd {
bool operator()(int a) { return (a % 2) == 1; }
};
my_list.remove_if(is_odd());
 
4) some other expression for which 'pred(a)' is well defined and
returns a bool:
 
bool remainder_is_one(int b, int a) { return (a % b) == 1; }
my_list.remove_if(std::bind(remainder_is_one, 2));
 
One of the reasons for introducing lambdas into the language was to
simplify the use of such "function objects", particularly when they
are simple expressions such as "is this an odd integer".
 
In your example code, the predicate passed to remove_if was an
anonymous instance of the struct is_odd that overrode the function
call operator with a single argument. (In my examples, I wrote the
argument declaration as 'int a', instead of 'const int &a', since it
isn't useful to pass integers by const reference.)
 
> mylist.remove_if (single_digit); // 15 36 17 20 39
 
This uses a pointer to the function single_digit.
 
> mylist.remove_if (is_odd()); // 36 20
 
This uses an anonymous (unnamed) instance of the struct is_odd.
 
Remember also that the primary difference between struct and class is
that class has private visibility by default and struct has public
visibility by default. Anything you can do with a class you can also
do with a struct.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
ram@zedat.fu-berlin.de (Stefan Ram): Apr 14 07:56PM

>Anyone have any idea why auto doesn't seem to work?
 
g++ ... -std=c++11 -D__USE_MINGW_ANSI_STDIO ...
Luca Risolia <luca.risolia@linux-projects.org>: Apr 14 09:55PM +0200

Il 09/04/2015 18:59, DeMarcus ha scritto:
> In the book and the example code it only brings up the universal
> reference T&& case.
 
I don't think so, see Item 28.
 
> }
 
> My conclusion was (I hope it's correct?) that std::forward always pass
> along lvalue references and move everything else.
 
Yes, more or less, as std::forward<T> does not really move anything.
Volker Wippel <Volker.Wippel@v2c2RemoveNoSpam.at>: Apr 14 11:49AM +0200

Hi
 
In C++11:
17.6.5.15 ...Unless otherwise specified, such moved-from objects
shall be placed in a valid but unspecified state.
 
std::vector<int> x = f();
auto const y = std::move(x);
x.push_back(1);... //use x like a "fresh" empty vector?
assert(1 == x.size());
 
For vectors or other STL containers and other classes, is this
"unspecified state" guarantied to allow save operations except
deletion? All or just move into, assignment?
 
Thanks, Volker
Jorgen Grahn <grahn+nntp@snipabacken.se>: Apr 14 10:42AM

On Tue, 2015-04-14, Volker Wippel wrote:
> auto const y = std::move(x);
> x.push_back(1);... //use x like a "fresh" empty vector?
> assert(1 == x.size());
 
The text you quote above contradicts your assertion.
x may just as well contain { 1, 2, 3 } at that point.
 
> For vectors or other STL containers and other classes, is this
> "unspecified state" guarantied to allow save operations except
> deletion? All or just move into, assignment?
 
Please clarify those questions; I can barely guess what you mean.
Do you mean "safe" rather than "save" for example?
 
I assume "valid but unspecified" means exactly that -- x is in one of
the infinite number of valid states cor a vector<int>, but you don't
know which one.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 14 07:53AM -0400

On 4/14/2015 5:49 AM, Volker Wippel wrote:
 
> For vectors or other STL containers and other classes, is this
> "unspecified state" guarantied to allow save operations except
> deletion?
 
See Jorgen's reply, and also my question:
Why not deletion? What prevents you from doing
x.clear();
to ensure it's empty before you start inserting into it?
 
> All or just move into, assignment?
 
Everything is OK as long as you don't assume it contains any useful
data. It might or it might not, that's the whole idea of "unspecified
state". Feel free to query its size as well. And if it does have some
data (x.empty() == false), you can extract those too, not sure of what
use they are going to be.
 
V
--
I do not respond to top-posted replies, please don't ask
"Öö Tiib" <ootiib@hot.ee>: Apr 14 09:16AM -0700

On Tuesday, 14 April 2015 12:50:13 UTC+3, Volker Wippel wrote:
 
> In C++11:
> 17.6.5.15 ...Unless otherwise specified, such moved-from objects
> shall be placed in a valid but unspecified state.
 
Others already answered your puzzle, I just add few
other related notes:
 
* Standard "specifies otherwise" sometimes; for example
moved from 'std::unique_ptr' is guaranteed to be in empty state.
* 'std::move' has misleading name since calling it does not
automatically result with its argument being moved from.
* Doing other things besides copying or moving in
user-defined copy or move constructor can work like
boobytrap because C++ program may elide moves and
copies in number of situations.
* Move in Visual C++ 2013 is not conforming so if it is
among target platforms then standard is not best source
of knowledge.
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: