Article View: comp.lang.c
Article #2050Re: variadic min and max, (any some and none, cat and more)
From: David Brown
Date: Tue, 01 Dec 2026 09:31
Date: Tue, 01 Dec 2026 09:31
107 lines
4611 bytes
4611 bytes
On 30/11/2026 22:34, Lawrence D’Oliveiro wrote:
> On Mon, 30 Nov 2026 20:47:38 +0100, Bonita Montero wrote:
>
>> Am 30.11.2026 um 19:49 schrieb Lawrence D’Oliveiro:
>>
>>> On Mon, 30 Nov 2026 19:44:50 +0100, Bonita Montero wrote:
>>
>>>> It's for sure more flexible since it can handle more variations of
>>>> compatible types and it's for sure much fasger.
>>
>>> You betcha:
>>>
>>> >>> max("red", "green", "blue")
>>> 'red'
>>> >>> max(["red"], ["red", "green"], ["blue"])
>>> ['red', 'green']
>>>
>>> Anything that has an ordering defined will work.
>>
>> In C++ you can write your own conversion to supply compatible types.
>
> Python does custom operator overloads, too. And multiple inheritance.
> And its core definition still ends up being only a small fraction of
> the size of C++.
>
> And unlike C++, and almost like Algol 68, it can do things that look
> like custom operators, too
> <https://gitlab.com/ldo/python_topics_notebooks/-/blob/master/Defining%20(Almost)%20Custom%20Operators.ipynb>.
You can do that just as well in C++ as in Python.
A 3D vector type in C++ is simple :
template<typename T>
struct Vector {
T x, y, z;
};
Vector v1 { 2, 3, 4 };
Vector v2 { 5, 4, 3 };
Making operator overloads for multiplication like your Python example,
with pairwise multiplication and multiplication by a scalar is standard
stuff :
template<typename T>
constexpr auto operator * (Vector<T> a, Vector<T> b) {
return Vector(a.x * b.x, a.y * b.y, a.z * b.z);
}
template<typename T>
constexpr auto operator * (Vector<T> a, T b) {
return Vector(a.x * b, a.y * b, a.z * b);
}
Vector v1_times_v2 = v1 * v2;
Vector v1_times_2 = v1 * 2;
Making an additional named operator is just a matter of a simple wrapper
struct whose type is used to change the meaning of operators. Then you
have a singleton type that is used to generate the wrapper.
template<typename T>
struct Crosser_wrapper : Vector<T> { };
struct Cross { };
constexpr Cross cross;
template<typename T>
constexpr auto operator * (const Vector<T> & a, Cross) {
return Crosser_wrapper(a);
}
template<typename T>
constexpr auto operator * (Crosser_wrapper<T> a,
Vector<T> b) {
return Vector(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
Vector v1cross = v1 *cross* v2;
Note that this is all zero-cost at runtime. No extra space needed, no
extra function calls generated, no need for the "cross" bit to hold
information or track references to the vector data, etc. It's just type
manipulation to change which function is called.
And if you want to, you can also do :
#define 𝘅 *cross*
Vector v1cross2 = v1 𝘅 v2;
Nice, to read, but hard to type.
(I am not saying that this is a good idea in real code, but it
demonstrates the technique. Use it if you like it. If anyone wants to
discuss this in more detail, look at more generalisations, make it
safer, etc., then a new thread in comp.lang.c++ would be more appropriate.)
Technical Details
Message-ID:
<11em45u$1fp0a$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11bes8b$1gncr$1@dont-email.me> <11ek7b2$j8f6$1@raubtier-asyl.eternal-september.org> <11ekfu0$mq5n$4@dont-email.me> <11ekg71$n5p7$1@raubtier-asyl.eternal-september.org> <11ekgf8$mq5n$8@dont-email.me> <11ekjsp$pmch$2@raubtier-asyl.eternal-september.org> <11ekq4q$snda$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: variadic min and max, (any some and none, cat and more) Date: Tue, 1 Dec 2026 10:31:41 +0100 Organization: A noiseless patient Spider Lines: 107 Message-ID: <11em45u$1fp0a$1@dont-email.me> References: <11bes8b$1gncr$1@dont-email.me> <11ek7b2$j8f6$1@raubtier-asyl.eternal-september.org> <11ekfu0$mq5n$4@dont-email.me> <11ekg71$n5p7$1@raubtier-asyl.eternal-september.org> <11ekgf8$mq5n$8@dont-email.me> <11ekjsp$pmch$2@raubtier-asyl.eternal-september.org> <11ekq4q$snda$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 01 Dec 2026 09:31:43 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1565706"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185vBIZwOiXXXALGAo7bJSr2OUkIRsjYRg="; posting-host="c0f3271de497b8d6a8f25db294917010" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:BuptUQ7JluAnWK8c+bDdPAfYjes= sha256:vQb5haHK+F5yRPtimMh6vtHTRCHy/6v9e7UOCKXAK2U= sha1:xi/Z8i1Bl/cqmjnY6l5nXQWzkmI= sha256:TrMSNWgWiA8MJ8ms5umTCG++P1E09RGcUVKy69s/pqE= In-Reply-To: <11ekq4q$snda$1@dont-email.me> Content-Language: en-GB, nb-NO