Saturday, October 31, 2015

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

fl <rxjwg98@gmail.com>: Oct 31 09:52AM -0700

Hi,
 
As I remember that the friendship of a class is not inherited, in the
following code, I don't understand the comment:
// these operators need access to the Query_base* constructor
 
I do see Query constructor:
Query(const Query &c)
 
but I do not see Query_base constructor.
 
BTW, this code snippet is from a C++ book.
Could you tell me how to understand the above comment?
Thanks,
 
............
class Query_base {
friend class Query;
protected:
typedef TextQuery::line_no line_no;
virtual ~Query_base() { }
private:
// eval returns the |set| of lines that this Query matches
virtual std::set<line_no>
eval(const TextQuery&) const = 0;
// display prints the query
virtual std::ostream&
display(std::ostream& = std::cout) const = 0;
};
 
 
// handle class to manage the Query_base inheritance hierarchy
class Query {
// these operators need access to the Query_base* constructor
friend Query operator~(const Query &);
friend Query operator|(const Query&, const Query&);
friend Query operator&(const Query&, const Query&);
public:
Query(const std::string&); // builds a new WordQuery
 
// copy control to manage pointers and use counting
Query(const Query &c): q(c.q), use(c.use) { ++*use; }
~Query() { decr_use(); }
Query& operator=(const Query&);
 
// interface functions: will call corresponding Query_base operations
std::set<TextQuery::line_no>
eval(const TextQuery &t) const { return q->eval(t); }
std::ostream &display(std::ostream &os) const
{ return q->display(os); }
"Öö Tiib" <ootiib@hot.ee>: Oct 31 01:43PM -0700

On Saturday, 31 October 2015 18:52:55 UTC+2, fl wrote:
 
> I do see Query constructor:
> Query(const Query &c)
 
> but I do not see Query_base constructor.
 
I have feeling that it can be typo in comment. Comment tries
to explain why these operators are made 'friend' of 'Query' but the
given reason is confusing. Most easy to find out why these actually
are made friends is perhaps to make these not friends (remove
friend declarations from class) and try to compile.
 
 
> BTW, this code snippet is from a C++ book.
 
C++ is quite infamous for hundreds of books about it that have low
quality and may mislead more than help.
fl <rxjwg98@gmail.com>: Oct 31 01:53PM -0700

On Saturday, October 31, 2015 at 4:43:43 PM UTC-4, Öö Tiib wrote:
 
> > BTW, this code snippet is from a C++ book.
 
> C++ is quite infamous for hundreds of books about it that have low
> quality and may mislead more than help.
Thanks.
Yes, I have tried to comment out 'friend' keyword. It cannot pass compiling.
In class Query, there is a private member q, which points to Query_base:
 
// interface functions: will call corresponding Query_base operations
std::set<TextQuery::line_no>
eval(const TextQuery &t) const
{ return q->eval(t); }
private:
Query(Query_base *query): q(query),
use(new std::size_t(1))
{}
Query_base *q;
 
 
 
 
I check below a derived class. There is a constructor:
BinaryQuery(Query left, Query right, std::string op)
 
Is it a derived class constructor?
 
There is operator (|: or, &: and etc.) on it.
The original code may not comment accurately.
 
 
........
class BinaryQuery: public Query_base {
protected:
BinaryQuery(Query left, Query right, std::string op):
lhs(left), rhs(right), oper(op) { }
 
// abstract class: BinaryQuery doesn't define eval
std::ostream& display(std::ostream &os) const
{ return os << "(" << lhs << " " << oper << " "
<< rhs << ")"; }
 
const Query lhs, rhs; // right- and left-hand operands
const std::string oper; // name of the operator
};
"Öö Tiib" <ootiib@hot.ee>: Oct 31 02:09PM -0700

On Saturday, 31 October 2015 22:53:18 UTC+2, fl wrote:
 
> Is it a derived class constructor?
 
> There is operator (|: or, &: and etc.) on it.
> The original code may not comment accurately.
 
 
It may be that the comment that you talk about wanted to say something
like: "the operators need access to Query::Query(Query_base*) that is
private and so are made friend.".
asetofsymbols@gmail.com: Oct 31 12:38AM -0700

is it possible define operator% as
m<t> operator%(m<t>& a, t (f)(t))
where the 2th arg of operator% is one function
f:t->t?
or
m<t> operator%(m<t> a, t operator f(t))
when the function for apply in one
unary operator f:t->t
asetofsymbols@gmail.com: Oct 31 12:51AM -0700

is it all ok in
a=b*c+k
where
m<t> a,b,c,k;
m<t> operator+(const m<t>& x, const m<t>& y)
{m<t> result;
....
return result;
}
 
m<t> operator*(const m<t>& x, const m<t>& y)
{m<t> result;
....
return result;
}
 
if one consider memory allocation
Thank you
asetofsymbols@gmail.com: Oct 31 01:57AM -0700

> m<t> operator%(m<t> a, t operator f(t))
> when the function for apply in one
> unary operator f:t->t
 
is possible one operator% as this
<template Q> m<Q> operator%(m<t> a, Q operator f(t))
where f is a function f:t->Q
one operator that has result one matrix
of one other type in template
asetofsymbols@gmail.com: Oct 31 03:14AM -0700

Above i mean
template <class q>
m<q> operator%(m<t> x, q (f)(t) )
 
Where f is a function t->q
In practice
get one matrix of element type t [x]
Create a matrix of element type q
has the same number elements
Matrix x and the same sizes
Apply to each element of x
f and fill the matrix result
Is possible in that way?
y=x%f
asetofsymbols@gmail.com: Oct 31 03:54AM -0700

template <class q>
m<q> operator%(m<t> x, q (f)(t) )
 
 
If f is one operator for me that
is not possible in C++
m<u32> y(2,3), x(2,3);
 
y=x%~;
Seem ugly
possibly better
y=~%x;
 
template <class q>
m<q> operator%( q (f)(t), m<t> a)
 
with f operator or function...
but in the case of operator
=~%
sound strange...
Paavo Helde <myfirstname@osa.pri.ee>: Oct 31 11:47AM -0500

asetofsymbols@gmail.com wrote in news:593ee2f2-a0cb-44c4-9778-
> f and fill the matrix result
> Is possible in that way?
> y=x%f
 
Yes this is possible (ssuming I have interpreted your mumbling
correctly):
 
#include <vector>
#include <cassert>
#include <cstdlib>
 
template<typename T>
std::vector<T> operator%(const std::vector<T>& v, T (*f)(T)) {
std::vector<T> result;
for(auto x: v) result.push_back((*f)(x));
return result;
}
 
int main() {
std::vector<int> v = {1, -2, 3, -4};
std::vector<int> w = v % std::abs;
assert(w[3]==4);
}
asetofsymbols@gmail.com: Oct 31 10:20AM -0700

Thank you
I wrote until now only something as
u32 f(u32 x){return 2*x;}
 
M<u32> x(2,3);
x.i(); x.apply(f);
asetofsymbols@gmail.com: Oct 31 11:18AM -0700

I wrote:
u32 f(u32 x){return 2*x;}
 
M<u32> x(2,3);
x.i(); x.apply(f);
-------
I have seen in matlab anonymous
functions something as
x.apply( @(z) 2*z);
are there anonymous function in C++?
Wouter van Ooijen <wouter@voti.nl>: Oct 31 10:04PM +0100

> functions something as
> x.apply( @(z) 2*z);
> are there anonymous function in C++?
 
google "c++ lambda"
 
Wouter
Paul <pepstein5@gmail.com>: Oct 31 01:46PM -0700

The code pasted below is a fairly standard implementation of a fairly standard problem -- finding the mth-to-last value in a linked list. I omitted the memory management aspects.
I don't understand why the destructor is never called. For example, a local copy of the linked list is created in the constructList() function. I thought the destructor would be called when it goes out of scope.
 
Thank you very much for your help.
 
Paul
 
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct linkedList
{
int val;
linkedList* next;
 
linkedList(int Val):val(Val), next(0)
{
 
}
 
~linkedList()
{
cout << "destructor called"; // This never executes.
 
}
 
};
 
// Construct a linked list from stdin
const linkedList* constructList( )
{
linkedList* headList;
linkedList* buildList;
 
int listItems;
bool first = true;
while(cin >> listItems)
{
if(first)
{
buildList = new linkedList(listItems);
headList = buildList;
first = false;
}
 
else
{
buildList->next = new linkedList(listItems);
buildList = buildList->next;
}
 
}
return headList;
}
 
void mth_to_last(const linkedList* const& list, int m)
{
if(m <= 0)
{
cout << "NIL";
return;
}
 
const linkedList* fast = list;
const linkedList* slow = list;
 
for(int i = 0; i < m; ++i)
{
if(fast)
fast = fast->next;
else
{
cout << "NIL";
return;
}
}
 
while(fast)
{
fast = fast->next;
slow = slow->next;
}
 
cout << slow->val;
}
 
int main() {
 
int m;
cin >> m;
cin.clear();
const linkedList* list = constructList();
mth_to_last(list, m);
 
return 0;
}
bartekltg <bartekltg@gmail.com>: Oct 31 10:01PM +0100

On 31.10.2015 21:46, Paul wrote:
> list is created in the constructList() function. I thought the
> destructor would be called when it goes out of scope.
 
> Thank you very much for your help.
 
Dynamically allocated object do not have a scope!
A pointer to linkedList does have a scope, but is is not
tidied to life of an objetc.
If you want to create a dynamic allocated object which life
is equal to the life of a pointer to this object, use smart
pointers. unique_ptr or shared_ptr (and then look to the
manual to see, how to construct pointers,; avoid new).
 
If you allocate something with new, you (in most cases)
have to delete is manually!
 
BTW, do not keep linked list as a pointer. You want to
wrap it into an object, a container. Then your container
have a destructor, where you can manually clean everything.
 
bartekltg
 
 
Jorgen Grahn <grahn+nntp@snipabacken.se>: Oct 31 05:22PM

On Fri, 2015-10-30, Geoff wrote:
>>in seeing the languages listed in chronological order?
>>There doesn't seem to be any order to the listing now.
 
> Alphabetical would be more logical.
 
However, what matters is what rules Wikipedia has around
such things.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
fl <rxjwg98@gmail.com>: Oct 31 08:09AM -0700

Hi,
 
I notice that the return value 'ret_lines' definition has no scope 'TextQuery'
as the internal variable 'has_val'. Is there any rule on such usage?
 
 
Thanks,
 
 
..........
// returns lines not in its operand's result set
set<TextQuery::line_no>
NotQuery::eval(const TextQuery& file) const
{
// virtual call through the Query handle to eval
set<TextQuery::line_no> has_val = query.eval(file);
 
set<line_no> ret_lines;
 
// for each line in the input file, check whether that line is in has_val
// if not, add that line number to ret_lines
for (TextQuery::line_no n = 0; n != file.size(); ++n)
if (has_val.find(n) == has_val.end())
ret_lines.insert(n);
return ret_lines;
}
"Öö Tiib" <ootiib@hot.ee>: Oct 31 08:51AM -0700

On Saturday, 31 October 2015 17:09:20 UTC+2, fl wrote:
 
> I notice that the return value 'ret_lines' definition has no scope 'TextQuery'
> as the internal variable 'has_val'. Is there any rule on such usage?
 
> Thanks,
 
 
You posted a slice of code that does not compile with C++ compiler
and what it means depends on code that you did not post.
We do not know what type names 'line_no' and 'TextQuery::line_no'
represent; seems these are aliases to some integer type.
 
 
Paavo Helde <myfirstname@osa.pri.ee>: Oct 31 11:31AM -0500

fl <rxjwg98@gmail.com> wrote in
> // virtual call through the Query handle to eval
> set<TextQuery::line_no> has_val = query.eval(file);
 
> set<line_no> ret_lines;
 
As far as we know from this excerpt, the names TextQuery::line_no and
line_no may refer to different types, or they may not. For example, if
TextQuery is a class and NotQuery is inherited from TextQuery, they would
most probably refer to the same type.
 
A decent programming IDE would provide you with a 'go to definition'
feature which would help you to see (with some confidence) if they are
the same or not.
 
hth
Paavo
fl <rxjwg98@gmail.com>: Oct 31 09:58AM -0700

On Saturday, October 31, 2015 at 12:32:19 PM UTC-4, Paavo Helde wrote:
> the same or not.
 
> hth
> Paavo
 
Thanks both of you. Both types have its respective typedef's. They are
different.
fl <rxjwg98@gmail.com>: Oct 31 07:13AM -0700

Hi,
 
I know cin is standard input. I see the following code, but I don't know when
the return value of cin is '0'.
 
Could you tell me that?
 
Thanks,
 
 
 
 
bool get_word(string &s1)
{
cout << "enter a word to search for, or q to quit: ";
cin >> s1;
if (!cin || s1 == "q") return false;
else return true;
}
Richard Damon <Richard@Damon-Family.org>: Oct 31 10:54AM -0400

On 10/31/15 10:13 AM, fl wrote:
> if (!cin || s1 == "q") return false;
> else return true;
> }
 
cin is a istream.
 
istreams define a member operator!() which is the equivalent to calling
fail() to see if something has gone wrong with the stream (like it
couldn't extract a string s1)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Oct 31 01:10PM +0100

On 10/31/2015 11:59 AM, Stefan Ram wrote:
 
> #include <iostream>
> #include <ostream>
 
> int main() { constexpr auto num{ 11 }; ::std::cout << num << '\n'; }
 
My gut feeling: None of these involve initializer_list, even though
they syntactically involve initializer-list (oh, the difference of a
hyphen: reminds me of Latin 1 versus Latin-1, not sure what that was).
 
They look like formally direct initializations.
 
And as far as I can recall the syntax
 
int x = {42};
 
(copy initialization, but with the braces so that it looks like an
initialization_list) has always been valid, from original C and onward.
 
So, my gut feeling is that you need something like
 
auto x = {42};
 
and then you'd be in initializer_list-land.
 
 
Cheers & hth.,
 
- Alf
Victor Bazarov <v.bazarov@comcast.invalid>: Oct 31 08:44AM -0400

On 10/31/2015 6:59 AM, Stefan Ram wrote:
 
> #include <iostream>
> #include <ostream>
 
> int main() { constexpr auto num{ 11 }; ::std::cout << num << '\n'; }
 
How do you know that the header <initializer_list> is NOT included by
some directive in <iostream>, <ostream> or any headers they include? If
it's included, it's not "implicit use", is it?
 
V
--
I do not respond to top-posted replies, please don't ask
ram@zedat.fu-berlin.de (Stefan Ram): Oct 31 10:59AM

The following program looks innocuous.
 
#include <iostream>
#include <ostream>
 
int main() { constexpr int num{ 11 }; ::std::cout << num << '\n'; }
 
Does the above constitute an »implicit use of the template
»std::initializer_list«? I am thinking of 8.5.4p2:
 
»The template std::initializer_list is not predefined; if the
header <initializer_list> is not included prior to a use of
std::initializer_list --- even an implicit use in which the
type is not named (7.1.6.4) --- the program is ill-formed.«
 
Or would only the following be such an implicit use?
 
#include <iostream>
#include <ostream>
 
int main() { constexpr auto num{ 11 }; ::std::cout << num << '\n'; }
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: