Article View: comp.lang.c
Article #2177Re: Iterators over slices and views and ranges analogs in C
From: David Brown
Date: Fri, 04 Dec 2026 14:03
Date: Fri, 04 Dec 2026 14:03
75 lines
4035 bytes
4035 bytes
On 04/12/2026 14:03, bartc wrote:
> On 04/12/2026 12:45, Bonita Montero wrote:
>> Am 04.12.2026 um 13:07 schrieb bartc:
>>
>>> #define typename(x) (_Generic((x),\
>>> int: "int",\
>>> unsigned int: "unsigned int",\
>>> const int: "const int",\
>>> default: "other"))
>> Compared to C++ generics this really, really sucks !
>>
>
> How would you do typename(x) in C++ then?
>
> That is, return the type of expression 'x' as a string (in some
> canonical form).
>
> This is not really generics, but reflection.
>
> In my language it would be:
>
> 'x.typestr'
>
> (With 'x.type' returning a unique ordinal for the type, which can be
> used for comparisons etc.)
>
"typeid(x)" would be your starting point for C++. The resulting
"type_info" object has a "name()" function and a "hash_code" value that
is approximately unique (i.e., there is a small risk of collisions), and
a class "type_index" that can be used for associative containers.
The "name" returned is implementation-defined - many compilers return
the type names used for C++ name mangling (so "i" for "int", "j" for
"unsigned int", etc.) rather than "int". Any user-defined names
(typedefs, struct tags, etc.) are not used here.
Note that given "int x; const int z;", when used in expressions -
including typeid() - the top-level qualifiers are removed and thus they
have the same typeid. (The extent to which a top-level qualifier like
"const" is viewed as part of an object's type, an attribute of the
object, or a feature of its declaration and/or definition, can be a
complicated matter in programming languages.)
But you are right that this is more about reflection, which was added to
C++ in C++26 :
#include <print>
#include <meta>
template <std::meta::info ref>
void show_type() {
std::print("{} is {}\n",
identifier_of(ref),
display_string_of(type_of(ref)));
}
#define showtype(x) show_type<^^x>()
int main(void) {
int x;
unsigned int y;
const int z = 3;
showtype(x);
showtype(y);
showtype(z);
}
Here the qualification of "z" is preserved, because we are taking a
reflection of the declared object at the source code level, rather than
using the object itself in an expression.
Technical Details
Message-ID:
<11euh6u$1kdbo$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<-kGdnSzB4bWEM4z2nZ2dnZfqn_WdnZ2d@giganews.com> <11esd12$mfam$1@raubtier-asyl.eternal-september.org> <11et39b$rj55$1@news.xmission.com> <11ett3b$1a41p$1@dont-email.me> <11euadh$1gp4t$1@dont-email.me> <11euckd$1ht9i$1@raubtier-asyl.eternal-september.org> <11eudm8$1igc3$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.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: Iterators over slices and views and ranges analogs in C Date: Fri, 4 Dec 2026 15:03:09 +0100 Organization: A noiseless patient Spider Lines: 75 Message-ID: <11euh6u$1kdbo$1@dont-email.me> References: <-kGdnSzB4bWEM4z2nZ2dnZfqn_WdnZ2d@giganews.com> <11esd12$mfam$1@raubtier-asyl.eternal-september.org> <11et39b$rj55$1@news.xmission.com> <11ett3b$1a41p$1@dont-email.me> <11euadh$1gp4t$1@dont-email.me> <11euckd$1ht9i$1@raubtier-asyl.eternal-september.org> <11eudm8$1igc3$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 04 Dec 2026 14:03:11 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1717624"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yIWIMKjrJiT3f8vJ54cKguRCJk/yu3AE="; posting-host="f8d429fc41b298b14a9203a87d5618c6" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:mC7IFhg865N9M60PTog9Erit5nk= sha256:6/RLiSGKFWIhMRJY/naGU4bIpLTUKYTEUD+u2tdyeBI= sha1:0WmiuCOMq6W6xctsA6vlf/bufxI= sha256:tjMOMGbHp3ZwOWJRNNS84pWLsDRigpVezNRPn2BRjz0= Content-Language: en-GB, nb-NO In-Reply-To: <11eudm8$1igc3$1@dont-email.me>