Monday, May 17, 2021

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

"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: May 17 04:05PM -0700

I don't think this should work at all! It should segfault.
_______________________________
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
 
 
struct person
{
person(std::string const& name, unsigned long id)
: m_name(name), m_id(id)
{
std::cout << "(" << this << ")->person::person(" << name << ",
" << id << ")\n";
}
 
~person()
{
std::cout << "(" << this << ")->person::~person(" << m_name <<
", " << m_id << ")\n";
}
 
void display() const
{
std::cout << "(" << this << ")->person::display(" << m_name <<
", " << m_id << ")\n";
}
 
std::string m_name;
unsigned long m_id;
};
 
 
struct database
{
struct key_id
{
using is_transparent = void; // Humm...
 
bool operator() (person const& p0, person const& p1) const
{
return p0.m_id < p1.m_id;
}
 
bool operator() (std::string const name, person const& p) const
{
return name < p.m_name;
}
 
bool operator() (person const& p, std::string const name) const
{
return p.m_name < name;
}
 
bool operator() (unsigned long id, person const& p) const
{
return id < p.m_id;
}
 
bool operator() (person const& p, unsigned long id) const
{
return p.m_id < id;
}
};
 
typedef std::set<person, key_id> set_t;
 
void add_entry(person const& p0)
{
m_set.insert(p0);
}
 
set_t::iterator const& search_id(unsigned long id) const
{
return m_set.find(id);
}
 
set_t::iterator const& search_id(std::string const& name) const
{
return m_set.find(name);
}
 
void display() const
{
std::cout << "\n(" << this << ")->database::display()\n";
std::cout << "_____________________________________\n";
std::for_each(m_set.begin(), m_set.end(), [](person const& p)
{
p.display();
});
std::cout << "_____________________________________\n\n";
}
 
set_t m_set;
};
 
 
int main()
{
{
database db;
 
db.add_entry(person("Chris", 42));
db.add_entry(person("Lisa", 23));
db.add_entry(person("Randall", 37));
 
std::cout << "_____________________________________\n\n";
 
{
database::set_t::iterator result = db.search_id(37);
if (result != db.m_set.end())
{
std::cout << "FOUND 37!\n";
(*result).display();
}
}
 
{
database::set_t::iterator result = db.search_id(137);
if (result == db.m_set.end()) std::cout << "137 NOT
FOUND!\n\n";
}
 
{
database::set_t::iterator result = db.search_id("Lisa");
if (result != db.m_set.end())
{
std::cout << "FOUND ""Lisa""\n";
(*result).display();
}
}
 
db.display();
}
 
return 0;
}
_______________________________
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: May 17 04:11PM -0700

On 5/17/2021 4:05 PM, Chris M. Thomasson wrote:
> I don't think this should work at all! It should segfault.
> _______________________________
[snip buggy code that seems to work in MSVC]
> _______________________________
 
Now, this version should go ahead and work? It seems to.
_________________________________
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
 
 
struct person
{
person(std::string const& name, unsigned long id)
: m_name(name), m_id(id)
{
std::cout << "(" << this << ")->person::person(" << name << ",
" << id << ")\n";
}
 
~person()
{
std::cout << "(" << this << ")->person::~person(" << m_name <<
", " << m_id << ")\n";
}
 
void display() const
{
std::cout << "(" << this << ")->person::display(" << m_name <<
", " << m_id << ")\n";
}
 
std::string m_name;
unsigned long m_id;
};
 
 
struct database
{
struct key_id
{
using is_transparent = void; // Humm...
 
bool operator() (person const& p0, person const& p1) const
{
return p0.m_id < p1.m_id;
}
 
bool operator() (std::string const name, person const& p) const
{
return name < p.m_name;
}
 
bool operator() (person const& p, std::string const name) const
{
return p.m_name < name;
}
 
bool operator() (unsigned long id, person const& p) const
{
return id < p.m_id;
}
 
bool operator() (person const& p, unsigned long id) const
{
return p.m_id < id;
}
};
 
typedef std::set<person, key_id> set_t;
 
void add_entry(person const& p0)
{
m_set.insert(p0);
}
 
set_t::iterator const search_id(unsigned long id) const
{
return m_set.find(id);
}
 
set_t::iterator const search_id(std::string const& name) const
{
return m_set.find(name);
}
 
void display() const
{
std::cout << "\n(" << this << ")->database::display()\n";
std::cout << "_____________________________________\n";
std::for_each(m_set.begin(), m_set.end(), [](person const& p)
{
p.display();
});
std::cout << "_____________________________________\n\n";
}
 
set_t m_set;
};
 
 
int main()
{
{
database db;
 
db.add_entry(person("Chris", 42));
db.add_entry(person("Lisa", 23));
db.add_entry(person("Randall", 37));
 
std::cout << "_____________________________________\n\n";
 
{
database::set_t::iterator const& result = db.search_id(37);
if (result != db.m_set.end())
{
std::cout << "FOUND 37!\n";
(*result).display();
}
}
 
{
database::set_t::iterator const& result = db.search_id(137);
if (result == db.m_set.end()) std::cout << "137 NOT
FOUND!\n\n";
}
 
{
database::set_t::iterator const& result = db.search_id("Lisa");
if (result != db.m_set.end())
{
std::cout << "FOUND ""Lisa""\n";
(*result).display();
}
}
 
db.display();
}
 
return 0;
}
_________________________________
MrSpook_qg11H@ihwz2zmflqtcalb.gov.uk: May 17 04:06PM

On Mon, 17 May 2021 17:58:16 +0200
>> or
>> pure virtual func();
 
>It's the same for me but not for compulsive personalities.
 
Oh ok. Well why use words at all then? Why not have something like
 
@@func() = 0;
 
C++ could end up like the explosion in an ascii factory that is objective-C.
 
>> it exists is to make life easier. Having moronic syntax has the opposite
>effect.
 
>That's not a practical reason.
 
A stupid response.
 
>> static int i = 123;
>> };
 
>There is a good reason because there's only one definition of i.
 
Ok, you don't understand the concept of initialisation. Thanks for clearing
that up.
 
>> All these "tiny" problems add up.
 
>No, they stand side by side.
 
And with attitudes like that C++ will continue to be shunned by new
generations of programmers.
Bonita Montero <Bonita.Montero@gmail.com>: May 17 06:21PM +0200


>> It's the same for me but not for compulsive personalities.
 
> Oh ok. Well why use words at all then? Why not have something like
> @@func() = 0;
 
That's a matter of habituation.
 
>> effect.
 
>> That's not a practical reason.
 
> A stupid response.
 
No, I asked for practical reasons and not metaphors.
 
 
>> There is a good reason because there's only one definition of i.
 
> Ok, you don't understand the concept of initialisation. Thanks for clearing
> that up.
 
There is a good reason for this to be not legal: the one-definition
-rule. If you want it the way which is up to your taste define the
variable inline and compile the code with C++17.
 
 
>> No, they stand side by side.
 
> And with attitudes like that C++ will continue to be shunned by new
> generations of programmers.
 
You didn't name any real problems which coudn't be solved in a blink
of an eye.
Juha Nieminen <nospam@thanks.invalid>: May 17 04:36PM

>>> the difference between their backsides and a type of donkey.
 
>>Just go wank whatever language you think is better, asshole.
 
> Oh dear, is it your time of the month buttercup? :)
 
Why don't you go fuck yourself, asshole?
Juha Nieminen <nospam@thanks.invalid>: May 17 04:37PM

> ,--.,',' ,'----.__\ _( \----'
> '///,`,--.,' `-.__.--' `. )
> '///,' `-`
 
Why don't you just go fuck yourself, asshole?
Juha Nieminen <nospam@thanks.invalid>: May 17 04:38PM

>> A stupid response.
 
> No, I asked for practical reasons and not metaphors.
 
Why do you even bother talking to the asshole?
Keith Thompson <Keith.S.Thompson+u@gmail.com>: May 17 10:37AM -0700


> If you don't like the language, then don't use it. It's that simple.
> You can just go and fuck off. Go wank to whatever other language you think
> is so much better.
 
Juha, I've just added "MrSpook" to my killfile. I'm seriously
considering adding you as well. Please knock it off.
 
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
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: