Monday, June 25, 2018

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

boltar@cylonHQ.com: Jun 25 08:59AM

Why is it I can do the following (even in C)...
 
int i[] = { 1,2,3 }
 
but in C++ 2011 (and newer?) I still can't do:
 
void func(int i[])
{
}
 
 
func({ 1,2,3 })
 
Why did the standards committee not permit this but instead the standards
committee came up with the new std::initializer_list (spelt wrong for everyone
outside the USA too) class? Surely allowing both would be advantagious?
 
Just curious.
Paavo Helde <myfirstname@osa.pri.ee>: Jun 25 02:19PM +0300

> {
> }
 
> func({ 1,2,3 })
 
This is because void func(int i[]) is the same than void func(int* i)
(by ancient C rules). How should the system know how to convert some
random initializer_list into a pointer.
 
As you see, the old C syntax is misleading and error-prone. Fortunately
in C++11 you can rewrite the above in a perfectly fine C++, with zero
overhead:
 
#include <array>
 
void func(const std::array<int,3>& i)
{
}
 
int main() {
func({ 1,2,3 });
}
Juha Nieminen <nospam@thanks.invalid>: Jun 25 12:56PM


> int main() {
> func({ 1,2,3 });
> }
 
Notice, however, that those values will be copied to a temporary array
(if I understand correctly how std::array works), rather than just
a pointer to a static array being passed to the function.
 
(Also, if I understand correctly,) this is different from the function
taking an initializer_list, in which case nothing will be copied, and
the pointer inside that object will directly point to that static
array (even if it's "temporary" in scope).
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jun 25 03:21PM +0200

On 25.06.2018 13:19, Paavo Helde wrote:
 
> int main() {
>     func({ 1,2,3 });
> }
 
Well that makes a dynamic copy, even it it's likely to be optimized away.
 
OP: the basic C++ stuff goes like this:
 
template< int n >
void func( const int (&a)[n] )
{}
 
...
func({ 1,2,3 });
 
But I prefer a few named type builders (which is entirely reusable code,
so the definitions would ordinarily not be there, just an `#include`):
 
#include <limits.h>
 
using Size = ptrdiff_t;
template< class Type > using ref_ = Type&;
template< Size n, class Item > using array_of_ = Item[n];
 
template< int n >
void func( ref_<const array_of_<n, int>> a )
{}
 
...
func({ 1,2,3 });
 
Since arrays are so special, the declarations
 
const array_of_<3, int>
 
and
 
array_of_<3, const int>
 
mean the same. The const-ness of an array is a const-ness of its items,
and vice versa. With `std::array` there is a type difference.
 
#include <array>
#include <limits.h>
#include <iostream>
#include <type_traits>
 
using Size = ptrdiff_t;
template< class Type > using ref_ = Type&;
template< Size n, class Item > using array_of_ = Item[n];
 
 
auto main()
-> int
{
using namespace std;
using A = const array_of_<3, int>;
using B = array_of_<3, const int>;
cout << (is_same_v<A, B>? "Identical" : "Different") << " raw
types.\n";
using C = const array<int, 3>;
using D = array<const int, 3>;
cout << (is_same_v<C, D>? "Identical" : "Different") << "
std::array types.\n";
}
 
Output:
 
Identical raw types.
Different std::array types.
 
So there are some differences between `std::array` and raw arrays,
include the (formal) data copying for `std::array` mentioned above, and
the type conflation for raw arrays shown by the last program.
 
Cheers!,
 
- Alf
boltar@cylonHQ.com: Jun 25 02:13PM

On Mon, 25 Jun 2018 14:19:04 +0300
 
>This is because void func(int i[]) is the same than void func(int* i)
>(by ancient C rules). How should the system know how to convert some
>random initializer_list into a pointer.
 
The same way it knows how to do it in the first definition. Why would this be
difficult to implement?
 
>As you see, the old C syntax is misleading and error-prone. Fortunately
>in C++11 you can rewrite the above in a perfectly fine C++, with zero
>overhead:
 
Unfortunately the problem with "perfectly fine" c++ is , as in this case, it
takes a C concept and adds a lot of syntactic noise to it plus some CPU
overhead to create the std::array objects that are being referenced.
 
Given the choice of "int i[]" and "const std::array<int,3>& i" I wouldn't
pick the latter, and if I did need the flexibility of std::array I'd
probably just use a vector anyway.
boltar@cylonHQ.com: Jun 25 02:17PM

On Mon, 25 Jun 2018 15:21:30 +0200
> auto main()
> -> int
 
Seriously?? Apart from that going against 40 years of convention the whole
point of auto functions is for when you DON'T know the return type in
advance.
Paavo Helde <myfirstname@osa.pri.ee>: Jun 25 07:37PM +0300

>> random initializer_list into a pointer.
 
> The same way it knows how to do it in the first definition. Why would this be
> difficult to implement?
 
The first usage
 
int i[] = { 1,2,3 };
 
actually defines an array, not a pointer. Try
 
int* i = { 1,2,3 };
 
and it gives the exact same error ("cannot convert from 'initializer
list' to 'int *'").
 
The fact that the two usage cases look the same but are not are due to
the problems with the old C syntax.
 
 
> Given the choice of "int i[]" and "const std::array<int,3>& i" I wouldn't
> pick the latter, and if I did need the flexibility of std::array I'd
> probably just use a vector anyway.
 
What you want is a temporary array, and both the end-points are
something else (an initializer list and a pointer). The compiler will
not try to guess what's in your mind, so you need to indicate somehow
you want to have array in-between. If you don't like to do that on the
callee site, you have to do this at the caller site:
 
void func(const int i[])
{
}
 
typedef const int(&a)[3];
 
int main() {
func( a { 1, 2, 3 });
}
 
(you need to add consts because C++ is trying to keep you from
accidentally modifying the temporary).
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Jun 25 07:52AM -0700

On Saturday, June 23, 2018 at 3:03:51 PM UTC-4, Kenny McCormack wrote:
 
> But Rickianity isn't.
 
> And what Rick preaches is the later, not the former (regardless of what
> terminology he uses).
 
Hey Kenny, which part of my teaching do you think doesn't align with Christianity?
 
> --
> "You can safely assume that you have created God in your own image when
> it turns out that God hates all the same people you do." -- Anne Lamott
 
Have you ever read what Napoleon wrote after studying world religions?
 
http://biblehub.com/library/schaff/the_person_of_christ/napoleon_bonaparte.htm
 
----[ Begin ]-----
 
Napoleon replied: --
 
"I know men; and I tell you that Jesus Christ is not a man. Superficial minds see a resemblance between Christ and the founders of empires, and the gods of other religions. That resemblance does not exist. There is between Christianity and whatever other religions the distance of infinity.
 
"We can say to the authors of every other religion, You are neither gods, nor the agents of the Deity. You are but missionaries of falsehood, moulded from the same clay with the rest of mortals. You are made with all the passions and vices inseparable from them. Your temples and your priests proclaim your origin.' Such will be the judgment, the cry of conscience, of whoever examines the gods and the temples of paganism.
 
...
 
-----[ End ]-----
 
If your God is not like the true Jesus Christ from the Bible ... get a new one.
 
--
Rick C. Hodgin
Juha Nieminen <nospam@thanks.invalid>: Jun 25 12:36PM


> I think you are confused. Virtual functions don't work if the
> receiver of a message send is a value ratther than through a
> pointer/referece.
 
Of course they work. (Well, unless you consider the implicit use of
'this' to be using the object "by pointer".)
 
//--------------------------------------------------------
class Base
{
public:
void foo() { anotherFunction(); }
 
protected:
virtual void anotherFunction()
{ defaultImplementation; }
};
 
class Derived: public Base
{
protected:
virtual void anotherFunction() override
{ customImplementation; }
};
 
...
 
Base obj1;
Derived obj2;
obj1.foo();
obj2.foo();
//--------------------------------------------------------
Juha Nieminen <nospam@thanks.invalid>: Jun 25 12:41PM


> 23 is not one obj is just one immediate i pass to the function
> (even if 23 is push in the stack it is one obj
> but the compiler not think so)
 
That's not exactly what's happening. If you change your "int&" to a
"const int&", you'll see that it compiles and works ok.
 
The reason why the non-const version doesn't compile is that the language
doesn't like non-const references to temporaries. This is disallowed
in the standard. (You can still ignore the standard and cast away
the constness inside that function, but then all bets are off, and
the standard doesn't guarantee what will happen. It might work, it
might crash, or it might do something else at random.)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jun 24 07:39AM +0200

I can't recall having seen this approach, so posting it here:
 
 
void cppmain()
{
using namespace std;
 
class With_faux_text
{
istringstream faux_input{ some_text() };
const ptr_<streambuf> original_buffer = cin.rdbuf();
public:
const char _{ '-' };
~With_faux_text() { cin.rdbuf( original_buffer ); }
With_faux_text() { cin.rdbuf( faux_input.rdbuf() ); }
};
 
class With_stream_detection
{
public:
const char _{ '-' };
With_stream_detection()
{
using namespace sys::io;
if( not is_connected( Stream_id::in ) ) { cin.setstate(
ios::failbit ); }
}
};
 
stdlib::ext::process::Command_line_args args;
(args[1] == "--faux-text"? With_faux_text{}._ :
With_stream_detection{}._), app::run();
}
 
 
Cheers!,
 
- Alf
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Jun 22 04:18PM -0400

On 6/22/2018 3:58 PM, Kenny McCormack wrote:
> every other person on the planet refers to as a "dog".
 
> So, from here on in, please refer to your product as "Rickianity", and
> everyone (yes, even you!) will be a lot happier.
 
 
Have you ever read these verses:
 
https://www.biblegateway.com/passage/?search=Matthew+7%3A21-23&version=KJV
 
21 Not every one that saith unto me, Lord, Lord, shall enter
into the kingdom of heaven; but he that doeth the will of
my Father which is in heaven.
22 Many will say to me in that day, Lord, Lord, have we not
prophesied in thy name? and in thy name have cast out devils?
and in thy name done many wonderful works?
23 And then will I profess unto them, I never knew you: depart
from me, ye that work iniquity.
 
In scripture, they didn't have bold font type, so they use repetition
to indicate significance or emphasis. They say that God is "holy,
holy, holy," for example. It's repeated three times to stress just
how holy God is. If it were in our modern writing, it would not only
be bold, but probably italicized, underlined, and in a font size that
is two or three up from the rest of the nearby text. It may even have
a different color background for highlighting.
 
Here we see Jesus saying, "Not everyone who says to me, Lord, Lord..."
This is Jesus effectively saying, "Not everyone who emphatically said
I was their Lord and Savior will be saved. Not everyone who lived
their life purportedly for me will be saved, but only those who do
the will of my Father who is in Heaven."
 
What is the Father's will? To lead people to His Son so they can be
saved. To live a holy life so that we can be servants of God in this
world, pursuing Him in spirit and in truth, denying our fleshly lusts,
and being at the constant, ready position to move when His spirit calls
us from within.
 
The reason you see an impotent church affecting NOTHING in this world,
being just like the world, doing the same things the world does, is
because they are not following Jesus Christ. They have a weak religion
and they are yet dead in their sin.
 
My message is simple, Kenny:
 
You have sin.
Your sin will condemn your eternal soul to Hell.
You need to be forgiven to be saved.
Jesus went to the cross so you could be forgiven.
He wants to forgive you. He asks you to ask Him to forgive
you so He can forgive you. He sends out people like me all
over this world to point you to Him.
 
Only those people who hear His call and answer it by repenting of
their sin and asking forgiveness from Jesus personally will be
saved.
 
And once again, Christianity is not a religion. It is a personal,
ongoing, daily, unending relationship with Jesus Christ. It's from
the inside out and it changes everything about you. That's the born
again nature. Religion is merely learned lessons from the outside
in. It has no saving power, even though it has the outward appearance
of being similar to the true saved state.
 
--
Rick C. Hodgin
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: