Wednesday, January 20, 2016

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

Daniel <danielaparker@gmail.com>: Jan 19 08:29PM -0800

On Monday, January 18, 2016 at 6:12:54 PM UTC-5, Mr Flibble wrote:
> All your favourite sausages can now be found on GitHub including
> segmented_array.
 
> https://github.com/FlibbleMr/neolib
 
Thanks for posting this.
 
I note that while array_tree has a stateful allocator, the swap method does not swap the allocator, but does swap pointers to data that were allocated with the allocator.
 
Is this safe?
 
Thanks,
Daniel
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jan 20 07:41PM

On 20/01/2016 04:29, Daniel wrote:
 
> Thanks for posting this.
 
> I note that while array_tree has a stateful allocator, the swap method does not swap the allocator, but does swap pointers to data that were allocated with the allocator.
 
> Is this safe?
 
Probably not; will investigate, thanks.
 
/Flibble
legalize+jeeves@mail.xmission.com (Richard): Jan 20 12:10AM

[Please do not mail me a copy of your followup]
 
Vir Campestris <vir.campestris@invalid.invalid> spake the secret code
 
>These days I do use Eclipse, and my apps are cross compiled to Android.
 
>Mostly. I do miss the ease of use of Visual Studio, and it's a curse
>that the Android build system really doesn't work well with Eclipse.
 
VS 2015 added Android/iOS cross-platform support (MS even made their
own Android emulator, I believe). I haven't tried it, so I have no
idea how good it is. I'd like to hear from a regular Android
developer as to the quality of the implementation.
 
>I suspect that myopia is the reason why STL files can (or is it could)
>only be opened with a narrow char name. Doesn't map at all well to
>Windows, but it's fine for Linux/Unix...
 
For that one, I think it's a matter of who is writing proposals to
the standards committee and how passionate they are about making their
proposals part of the standard library. It looks like we're going to
get some version of boost::filesystem in the standard soon and I think
that addresses this shortcoming, but I am not certain.
--
"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>
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jan 20 07:39PM

On Mon, 2016-01-18, Richard wrote:
...
> LGPL generally means that you can link against it without open
> sourcing your application.
 
> (Which is why RMS hates the LGPL.)
 
[citation needed]
 
Sure, he tries to convince people to use the GPL instead:
https://www.gnu.org/licenses/why-not-lgpl.html
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Christopher Pisz <nospam@notanaddress.com>: Jan 20 11:44AM -0600

It's been an eon since I had to use these manipulators. I am not getting
the output I expected. I am looking for a 2 digit month next to a 2
digit year.
 
 
#include <iostream>
#include <sstream>
#include <iomanip>
 
int main()
{
int month = 1;
int year = 2016;
 
std::ostringstream monthYearAsText;
monthYearAsText << std::setw(2) << std::setfill('0') << std::right
<< month
<< std::setw(2) << std::setfill('0') << std::right
<< year;
 
std::cout << monthYearAsText.str() << std::endl;
 
system("pause");
 
return 0;
}
 
Output:
012016
 
Expected Output:
0116
 
Where am I going wrong?
 
 
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---
Bo Persson <bop@gmb.dk>: Jan 20 07:06PM +0100

On 2016-01-20 18:44, Christopher Pisz wrote:
 
> Expected Output:
> 0116
 
> Where am I going wrong?
 
The width (setw) is considered a minimum width, and the stream uses the
fill and right attributes to decide the padding up to this width.
 
However, if the output is already >= the requested width, it is just
output as is. Even if it is wider.
 
 
Bo Persson
scott@slp53.sl.home (Scott Lurndal): Jan 20 06:13PM


>Where am I going wrong?
 
You should be using snprintf :-)
 
char monthyearastext[5];
snprintf(monthyearastext, sizeof(monthyearastext), "%2.2u%2.2u", month, year);
 
or strftime if you're starting from a struct tm.
Christof Warlich <christof.warlich1@gmail.com>: Jan 19 11:45PM -0800

Hi,
 
can anyone tell what's wrong with the friend declaration below and how to do it properly?
 
$ cat tst.cpp
template<typename T> class B;
class A {
class Inner {};
friend class B<Inner>;
};
template<typename T> class B {};
class Derived: public B<A::Inner> {};
 
$ g++ -c tst.cpp
tst.cpp:3:11: error: 'class A::Inner' is private
class Inner {};
^
tst.cpp:9:23: error: within this context
class Derived: public B<A::Inner> {};
^
 
Thanks for your kind attention,
 
Chris
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 10:42AM +0100

On 1/20/2016 8:45 AM, Christof Warlich wrote:
> class Derived: public B<A::Inner> {};
> ^
 
> Thanks for your kind attention,
 
The error message says that `A::Inner` is private.
 
Checking the definition of `A`, sure, class `A::Inner` is private, so
you can't normally access it or refer to it from other code than `A`.
 
It's private because `private` access is the default for `class`. If you
want `public` access as default, use a `struct`.
 
 
Cheers & hth.,
 
- Alf
"Öö Tiib" <ootiib@hot.ee>: Jan 20 02:09AM -0800

On Wednesday, 20 January 2016 09:46:09 UTC+2, Christof Warlich wrote:
> tst.cpp:9:23: error: within this context
> class Derived: public B<A::Inner> {};
> ^
 
Perhaps you misunderstand to whom and what access the 'friend' keyword
grants. The 'B<A::Inner>' has access to everything in 'A' thanks to that
'friend'. 'Inner' has access to everything in 'A' anyway, no need to 'friend' it.
The 'Derived' does not have access to private components of 'A' so can't
use 'A:Inner' and so can't define 'B<A:Inner>' as its base class.
That is what the compiler complains about.
Christof Warlich <christof.warlich1@gmail.com>: Jan 20 02:45AM -0800

Am Mittwoch, 20. Januar 2016 11:10:06 UTC+1 schrieb Öö Tiib:
> Perhaps you misunderstand to whom and what access the 'friend' keyword
> grants. The 'B<A::Inner>' has access to everything in 'A' thanks to that
> 'friend'.
 
This is why I was puzzled that I got the error message: As I made B<A::Inner> a friend of A, why does the compiler complain? Looks like using A::Inner as a template parameter requires access permissions to Inner from global scope, not from B<A::Inner>.
 
But even after this insight, the second part of my question remains: How to do it properly, i.e. how can I avoid the compile time error _without_ making class Inner public?
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 12:03PM +0100

On 1/20/2016 11:45 AM, Christof Warlich wrote:
 
> [snip]
> How to do it properly, i.e. how can I avoid the compile time error
> _without_ making class Inner public?
 
You just need to make the right friends instead of the wrong friends.
 
I.e., make `Derived` a `friend` of `A`, so that it can access `A::Inner`.
 
The public class' public inheritance from private `A::Inner` is,
however, a design smell – are you sure this is what you want?
 
 
Cheers & hth.,
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 12:06PM +0100

On 1/20/2016 12:03 PM, Alf P. Steinbach wrote:
> [snip]
> The public class' public inheritance from private `A::Inner` is,
> however, a design smell – are you sure this is what you want?
 
Oh, sorry, it's just a template parameter of the base class type.
 
Still, it looks a bit weird, like a solution to some other unmentioned
problem?
 
 
Cheers,
 
- Alf
"Öö Tiib" <ootiib@hot.ee>: Jan 20 03:23AM -0800

On Wednesday, 20 January 2016 12:45:56 UTC+2, Christof Warlich wrote:
 
> But even after this insight, the second part of my question remains: How to do it properly, i.e. how can I avoid the compile time error _without_ making class Inner public?
 
Hard to tell what you want to make public or what not. However I try to
use deductive logic. Since 'B<A::Inner>' is public base class (so those
base objects of 'Derived' objects are public) you most likely want to
make type of those ('B<A::Inner>') also public. IOW:
 
template<typename T> class B;
class A {
class Inner {};
public:
using Binner = B<Inner>;
};
template<typename T> class B {};
class Derived: public A::Binner {};
Christof Warlich <christof.warlich1@gmail.com>: Jan 20 03:26AM -0800

Am Mittwoch, 20. Januar 2016 12:06:33 UTC+1 schrieb Alf P. Steinbach:
> Oh, sorry, it's just a template parameter of the base class type.
 
Yeah, making Derived a friend did not do the trick. So there is really no way except of making Inner public?
 
 
> Still, it looks a bit weird, like a solution to some other unmentioned
> problem?
 
Well, I'm extending std::thread to accept an additional parameter, i.e. POSIX attributes, and B:<Inner> in reality is a member from the std namespace that I cannot change.
 
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 12:56PM +0100

On 1/20/2016 12:26 PM, Christof Warlich wrote:
>> Oh, sorry, it's just a template parameter of the base class type.
 
> Yeah, making Derived a friend did not do the trick. So there is
> really no way except of making Inner public?
 
This compiles nicely with g++ 4.8.2 and MSVC 2015:
 
template<typename T> class B;
 
class A {
class Inner {};
friend class Derived;
};
 
template<typename T> class B {};
 
class Derived: public B<A::Inner> {};
 
auto main() -> int
{
Derived x;
(void) x;
}
 
 
 
> Well, I'm extending std::thread to accept an additional parameter,
> i.e. POSIX attributes, and B:<Inner> in reality is a member from the
> std namespace that I cannot change.
 
Ah.
 
 
Cheers & hth.,
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 12:57PM +0100

On 1/20/2016 12:56 PM, Alf P. Steinbach wrote:
 
> This compiles nicely with g++ 4.8.2 and MSVC 2015:
 
Dang, 5.1.0
Christof Warlich <christof.warlich1@gmail.com>: Jan 20 04:21AM -0800

Am Mittwoch, 20. Januar 2016 12:58:04 UTC+1 schrieb Alf P. Steinbach:
> On 1/20/2016 12:56 PM, Alf P. Steinbach wrote:
 
> > This compiles nicely with g++ 4.8.2 and MSVC 2015:
 
> Dang, 5.1.0
 
I still get the same error with gcc 4.8.4. Interesting to know that it works with gcc 5.1.0 and MSVC 2015.
Christof Warlich <christof.warlich1@gmail.com>: Jan 20 04:24AM -0800

Am Mittwoch, 20. Januar 2016 12:24:28 UTC+1 schrieb Öö Tiib:
> };
> template<typename T> class B {};
> class Derived: public A::Binner {};
 
Thanks, that works with gcc 4.8.4, so that seems to be what I was looking for :-).
Martin Shobe <martin.shobe@yahoo.com>: Jan 20 06:22AM -0600

On 1/18/2016 3:59 AM, Juha Nieminen wrote:
> in cases where the code just doesn't care what the type is, or when there
> is some guarantee about the behavior of the type, but the exact name of the
> type is irrelevant (iterators are a perfect example.)
 
I mostly agree with the above. I also use it when the type is already
obvious. For example
 
auto foo = new bar{};
 
Martin Shobe
Rosario19 <Ros@invalid.invalid>: Jan 20 06:56AM +0100

On Tue, 19 Jan 2016 20:34:49 +0100, "Alf P. Steinbach" wrote:
 
>The following piece of code smells ungood to me, even though I wrote it
>myself:
 
> switch( buffer[n_chars-1] )
 
so are you sure that n_chars>0
 
> {
> case Syschar( Ascii::lf ):
 
what about malformated text that has 3 lf only without CR
 
"Lőrinczy Zsigmond" <zsiga@nospam.for.me>: Jan 20 10:14AM +0100

If I were you, I'd simply write this:
 
if (n_chars>0 && buffer[n_chars-1]=='\n') buffer[--n_chars]= '\0';
if (n_chars>0 && buffer[n_chars-1]=='\r') buffer[--n_chars]= '\0';
 
I know it is dissappontingly simple...
(Also it doesn't handle the \n\r situation)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 10:47AM +0100

On 1/19/2016 9:52 PM, Marcel Mueller wrote:
>> default: {}
>> }
 
> Will not work with old MAC format (CR only).
 
Right. As shown by the context code, this is Windows direct console i/o.
 
 
>> I probably need much more coffee in order to clean that up, but then I
>> wouldn't sleep tonight. So, asking.
 
> So what is your question?
 
I was thinking that decrementing n_chars and then incrementing it is a
smell of imperfect logic...
 
 
>> console i/o.
 
> EOF might not be that uncommon with console i/o if i/o redirection takes
> place.
 
The direct console streams in Windows can't be easily redirected, it's
similar to reading /dev/tty in Unixland (if I recall correctly from
umpteen decades back).
 
In the early DOS days there was support for using an external serial
terminal instead of the monitor, but I don't think that was brought
forward into Windows.
 
 
Cheers, & thanks,
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 10:48AM +0100

On 1/20/2016 6:56 AM, Rosario19 wrote:
>> myself:
 
>> switch( buffer[n_chars-1] )
 
> so are you sure that n_chars>0
 
Yes, from the
 
if( n_chars == 0 )
{
return 0; // End of file. Should not happen with
console i/o.
}
 
 
right before.
 
 
Cheers, & thanks,
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jan 20 11:01AM +0100

On 1/20/2016 10:14 AM, Lőrinczy Zsigmond wrote:
> if (n_chars>0 && buffer[n_chars-1]=='\r') buffer[--n_chars]= '\0';
 
> I know it is dissappontingly simple...
> (Also it doesn't handle the \n\r situation)
 
Well since I didn't much explain the code you misunderstood it. :( Mea
culpa. Sorry.
 
This is Windows code that always gets \r\n up from the API level. The
separate testing for \r was not to handle out-of-order \n\r, but because
it can happen that the buffer doesn't have room for the ordinary \r\n's
final \n, in which case that single value is delivered via the next
read. Whichever of the two situations happen, the received control chars
are removed, and a single \n is added at the end of a complete line --
or at least that was the intention, and it seems to work (I use a very
small buffer, just 6 values, to test it). :)
 
The idea of nulling is good in general but for this code it's necessary
to keep explicit track of the length to avoid checking the length again,
and nulling isn't simpler than decrementing, is it?
 
 
Cheers, and thanks,
 
- Alf
 
PS: This brings up the question of how to unit-test such code
automatically, which seems to boil down to how to mock the API read
function (in this case Windows' ReadConsoleW) in a good way, maintaining
the maintainability of the code, so to speak?
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: