Thursday, June 11, 2015

Digest for comp.lang.c++@googlegroups.com - 17 updates in 7 topics

Paul <pepstein5@gmail.com>: Jun 11 02:35PM -0700

The below is quoted from a C++ text. However, I have been unable to implement the idea. Whenever I try, I get the warning for a function returning a reference to a local variable and a runtime error. Does anyone see how to make the idea work?
 
For example, a typical simple-as-possible findMax implementation would be something like
 
int findMax(std::vector<int> vec)
{
if(vec.empty())
throw std::runtime_error("Empty vector");
 
int max = vec[0];
for(int i = 1; i < vec.size(); ++i)
if(vec[i] > max)
max = vec[i];
 

return max;

}
 
If we change the signature to return a reference, I understand perfectly that we get an error because we're returning a reference to the local variable max. But if we don't do that, what does the author have in mind?
 
Many thanks for your help.
 
BEGIN QUOTE
Suppose we have a function findMax that returns the largest value in a vector or other large collection. Then given a vector arr, if we invoke findMax, we would naturally write
auto x = findMax( arr );
However, notice that if the vector stores large objects, then the result is that x will be a copy of the largest value in arr. If we need a copy for some reason, that is fine; however, in many instances, we only need the value and will not make any changes to x. In such a case, it would be more efficient to declare that x is another name for the largest value in arr, and hence we would declare x to be a reference (auto will deduce constness; if auto is not used, then typically a non-modifiable reference is explicitly stated with const):
auto & x = findMax( arr );
Normally,this means that findMax would also specify a return type that indicates a reference variable.
END QUOTE
Paul <pepstein5@gmail.com>: Jun 11 03:07PM -0700

On Thursday, June 11, 2015 at 10:54:39 PM UTC+1, Stefan Ram wrote:
> ::std::cout << get2( a )<< '\n'; }
 
> The object a[ 2 ] has a lifetime that transcends
> the lifetime of get2( a ).
 
Thanks. Do you know how to implement the idea in the quoted passage?
 
Paul
Ben Bacarisse <ben.usenet@bsb.me.uk>: Jun 11 11:51PM +0100

> perfectly that we get an error because we're returning a reference to
> the local variable max. But if we don't do that, what does the author
> have in mind?
 
I suspect the author is thinking of something like
 
const int& findMax(const std::vector<int>& vec)
{
if(vec.empty())
throw std::runtime_error("Empty vector");

int maxi = 0;
for(int i = 1; i < vec.size(); ++i)
if(vec[i] > vec[maxi])
maxi = i;
return vec[maxi];
}
 
<snip>
 
--
Ben.
ram@zedat.fu-berlin.de (Stefan Ram): Jun 11 08:58PM

>I'd like to know if it's possible to initialize a class w/o passing in a
>type to the template
 
#include <iostream>
#include <ostream>
#include <string>
#include <memory>
 
using namespace ::std::literals;
 
template< typename ... >struct Alpha { ::std::string s = "base"s; };
template<> struct Alpha<> { ::std::string s = "noarg"s; };
template< typename T >struct Alpha< T > { ::std::string s = "arg"s; };
 
int main()
{ ::std::cout << Alpha<>{}.s << '\n' << Alpha< int >{}.s << '\n'; }
 
/* prints:
noarg
arg */
ram@zedat.fu-berlin.de (Stefan Ram): Jun 11 09:54PM

>we would declare x to be a reference
 
You can return a reference.
 
The reference does not have to be a reference
to a local variable.
 
It also might be a reference to an object that
has a sufficient lifetime (or a subobject of
such an object). For example:
 
#include <iostream>
#include <ostream>
#include <array>
 
int & get2( ::std::array< int, 5 > a )
{ return a[ 2 ]; }
 
int main()
{ ::std::array< int, 5 >a ={ 4, 12, 22, 17, 41 };
::std::cout << get2( a )<< '\n'; }
 
The object a[ 2 ] has a lifetime that transcends
the lifetime of get2( a ).
David Brown <david.brown@hesbynett.no>: Jun 11 01:51PM +0200

On 11/06/15 12:31, gwowen wrote:
> anyone who cared about the projective real line and field axioms
> would just parse it mentally. FWIW: on the hated google group
> interace, that UTF-8 shows up correctly.
 
It's also fine here in Thunderbird on Linux.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jun 11 07:18PM +0100

On 11/06/2015 08:31, gwowen wrote:
 
> Sure, in \mathbb{R} it is undefined. But real mathematicians don't limit themselves to a single model of numbers, if other models are better for their purposes.
 
> On the real projective line \mathbb{R}^*, you can absolutely have 1/0 = infinity. The numbers stop being a field of course, but IEEE floats aren't a field either.
 
> Since IEEE floats can't reasonably model \mathbb{R} while the field axioms, a mathematician might very reasonably decide they might as well model the projective line instead.
 
Nonsense. In maths 1/0 is undefined not infinity. IEEE floating point
is wrong about this and about negative zero.
 
/Flibble
gwowen <gwowen@gmail.com>: Jun 11 11:27AM -0700

On Thursday, June 11, 2015 at 7:18:29 PM UTC+1, Mr Flibble wrote:
 
> > Since IEEE floats can't reasonably model \mathbb{R} while the field axioms, a mathematician might very reasonably decide they might as well model the projective line instead.
 
> Nonsense. In maths 1/0 is undefined not infinity. IEEE floating point
> is wrong about this and about negative zero.
 
Is there no subject on which your confidence does not exceed your knowledge?
 
Go read up on how the mathematical object called the Real Projective Line is defined. You may learn something. As Earl Weaver said, the important stuff is what you learn after you know everything.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jun 11 07:42PM +0100

On 11/06/2015 19:27, gwowen wrote:
>> is wrong about this and about negative zero.
 
> Is there no subject on which your confidence does not exceed your knowledge?
 
> Go read up on how the mathematical object called the Real Projective Line is defined. You may learn something. As Earl Weaver said, the important stuff is what you learn after you know everything.
 
The real projective line looks like nonsense to me mate. 1/0 is undefined.
 
/Flibble
Paavo Helde <myfirstname@osa.pri.ee>: Jun 11 03:43PM -0500

Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote in
 
> Nonsense. In maths 1/0 is undefined not infinity. IEEE floating point
> is wrong about this and about negative zero.
 
As this is a C++ group, math is no concern and neither is whether IEEE is
conceptually wrong in anything or not. If the C++ implementation is using
IEEE, that's what we have got and that's what we have to work with:
 
>echo '
#include <iostream>
int main() {
double x = 0;
std::cout << 1/x << "\n";
}
' > test1.cpp
>g++ test1.cpp
>./a.exe
inf
 
See there. It is inf, regardless or whether you or me like it or not (I
do!)
 
Cheers
Paavo
gwowen <gwowen@gmail.com>: Jun 11 01:45PM -0700

On Thursday, June 11, 2015 at 7:42:10 PM UTC+1, Mr Flibble wrote:
 
> The real projective line looks like nonsense to me mate. 1/0 is undefined.
 
(... shakes head sadly ...)
 
There is little worse than a man who is proudly ignorant.
Joe <no.spam@noemail.com>: Jun 11 02:25PM -0500

I have a template that I instantiate fine when passing a class type. I
understand the template needs a type to instantiate a class. What I'm
wondering is how to instantiate a template by passing a null because in
one situation, I only need to make use of a few methods of the class
that don't use the passed in template type. In otherwords, I'd like to
use it as a normal class and thought maybe I could just pass in a null
as the template arg.
 
Here's my latest try ...
 
std::unique_ptr<MyClass<std::nullptr_t>> mc;
 
mc = std::unique_ptr<MyClass<std::nullptr_t>> {
new MyClass<std::nullptr_t>(nullptr);
};
 
On 06/11/2015 12:02 PM, Victor Bazarov wrote:
Doug Mika <dougmmika@gmail.com>: Jun 11 11:36AM -0700

On Thursday, June 11, 2015 at 11:33:23 AM UTC-5, Doug Mika wrote:
> t2.join();
> cout << res1 << ' ' << res2 << '\n';
> }
 
Well, I haven't gotten my MinGW to compile any std::thread program, but I found a group on sourceforge dedicated to MinGW, so I hope I'll find out what the problem is soon enough. For now, I'm using the tutorialspoint.com online C++ shell for smaller activities. I was thinking of getting Microsoft's VC++ lite, but I heard they don't follow the C++11 standard on everything...I only heard that. Ideally, I'm looking for something that will let me read my Stroustrup book and do the work with a compiler that complements the theory
 
Doug
Victor Bazarov <v.bazarov@comcast.invalid>: Jun 11 02:48PM -0400

On 6/11/2015 2:36 PM, Doug Mika wrote:
> tutorialspoint.com online C++ shell for smaller activities. I was
> thinking of getting Microsoft's VC++ lite, but I heard they don't
> follow the C++11 standard on everything...I only heard that.
 
Right, so you formed a prejudice against them before even trying their
product. It's not the "VC++ lite", it's called "Express Edition". You
can get their 2013 Express Edition for Windows Desktop, it's pretty
close to C++11. You can also find what exactly is and what isn't
implemented from C++11 in it; they actually don't mind telling you, if
you care to see.
 
> Ideally, I'm looking for something that will let me read my
> Stroustrup book and do the work with a compiler that complements the
> theory
 
Presumably you have some result in mind, you're probably working towards
actually using your skills for something. Learning in a vacuum is not
useful. Do you know what you're going to be doing in C++ once you have
learned it all? Then start doing that something today! And for that
you should get yourself a compiler and stick with it. Be it G++ or
MSVC, or any other, whether paid for or free, they all differ slightly,
but don't that stop you from getting at least something. Also consider
a set of tools along with it, consisting of a good editor, a debugger, a
profiler (nice if it can handle threads). And plug away!
 
V
--
I do not respond to top-posted replies, please don't ask
Ian Collins <ian-news@hotmail.com>: Jun 12 07:11AM +1200

Doug Mika wrote:
> Ideally, I'm looking for something that will let me read my
> Stroustrup book and do the work with a compiler that complements the
> theory
 
Install Cygwin and g++.
 
--
Ian Collins
Victor Bazarov <v.bazarov@comcast.invalid>: Jun 11 01:28PM -0400

On 6/11/2015 1:11 PM, Paul wrote:
> The below appears (to me) to contain an error. Please could someone
confirm if there's an error? Otherwise, I need to review my
understanding of some basics. Thank you. I would have thought that &x
denotes the address of a variable and is therefore not an lvalue. This
is not given as an error in the online errata list.
> vector<string> *ptr = &arr;
 
> With these declarations, arr, str, arr[x], &x, y, z, ptr, *ptr, (*ptr)[x] are all lvalues.
> END QUOTE
 
Your understanding is correct. '&x' is not an lvalue. It's an
expression that yields an rvalue (or rather a prvalue in modern C++ terms).
 
V
--
I do not respond to top-posted replies, please don't ask
Ian Collins <ian-news@hotmail.com>: Jun 11 03:14PM +1200

fl wrote:
 
> Thanks for the prompt reply. I am still new to C++. What and how many
> ways to use the C result (data block) in C++? Could you give me a little
> more explanation?
 
The same way as you would use it in C, unless you want to do something
in a C++ way.
 
--
Ian Collins
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: