fanf: (dotat)
[personal profile] fanf

One of the new features in C99 was tgmath.h, the type-generic mathematics library. (See section 7.22 of n1256.) This added ad-hoc polymorphism for a subset of the library, but C99 offered no way for programmers to write their own type-generic macros in standard C.

In C11 the language acquired the _Generic() generic selection operator. (See section 6.5.1.1 of n1570.) This is effectively a type-directed switch expression. It can be used to implement APIs like tgmath.h using standard C.

(The weird spelling with an underscore and capital letter is because that part of the namespace is reserved for future language extensions.)

When const is ugly

It can often be tricky to write const-correct code in C, and retrofitting constness to an existing API is much worse.

There are some fearsome examples in the standard library. For instance, strchr() is declared:

    char *strchr(const char *s, int c);

(See section 7.24.5.2 of n1570.)

That is, it takes a const string as an argument, indicating that it doesn't modify the string, but it returns a non-const pointer into the same string. This hidden de-constifying cast allows strchr() to be used with non-const strings as smoothly as with const strings, since in either case the implicit type conversions are allowed. But it is an ugly loop-hole in the type system.

Parametric constness

It would be much better if we could write something like,

    const<A> char *strchr(const<A> char *s, int c);

where const<A> indicates variable constness. Because the same variable appears in the argument and return types, those strings are either both const or both mutable.

When checking the function definition, the compiler would have to treat parametric const<A> as equivalent to a normal const qualifier. When checking a call, the compiler allows the argument and return types to be const or non-const, provided they match where the parametric consts indicate they should.

But we can't do that in standard C.

Or can we?

When I mentioned this idea on Twitter a few days ago, Joe Groff said "_Generic to the rescue", so I had to see if I could make it work.

Example: strchr()

Before wrapping a standard function with a macro, we have to remove any existing wrapper. (Standard library functions can be wrapped by default!)

    #ifdef strchr
    #undef strchr
    #endif

Then we can create a replacement macro which implements parametric constness using _Generic().

    #define strchr(s,c) _Generic((s),                    \
        const char * : (const char *)(strchr)((s), (c)), \
        char *       :               (strchr)((s), (c)))

The first line says, look at the type of the argument s.

The second line says, if it is a const char *, call the function strchr and use a cast to restore the missing constness.

The third line says, if it is a plain char *, call the function strchr leaving its return type unchanged from char *.

The (strchr)() form of call is to avoid warnings about attempts to invoke a macro recursively.

    void example(void) {
        const char *msg = "hello, world\n";
        char buf[20];
        strcpy(buf, msg);

        strchr(buf, ' ')[0] = '\0';
        strchr(msg, ' ')[0] = '\0';
        strchr(10,20);
    }

In this example, the first call to strchr is always OK.

The second call typically fails at runtime with the standard strchr, but with parametric constness you get a compile time error saying that you can't modify a const string.

Without parametric constness the third call gives you a type conversion warning, but it still compiles! With parametric constness you get an error that there is no matching type in the _Generic() macro.

Conclusion

That is actually pretty straightforward, which is nice.

As well as parametric constness for functions, in the past I have also wondered about parametric constness for types, especially structures. It would be nice to be able to use the same code for read-only static data as well as mutable dynamic data, and have the compiler enforce the distinction. But _Generic() isn't powerful enough, and in any case I am not sure how such a feature should work!

Page 1 of 3 << [1] [2] [3] >>

Date: 2016-06-01 16:15 (UTC)
gerald_duck: (ascii)
From: [personal profile] gerald_duck
C++ gets you some of the way there simply by allowing function overloading:
inline const char *strchr(const char *s, char c) { return strchr(const_cast<char *>(s),c); }
You can go further and template the function:
template <typename T, typename C>
inline T strchr(T t, C c) { for(;*t;++t) if (*t==c) return t; return T(); }
…though you're beginning to reinvent std::string::find() and std::find() at that point.

One thing I'd very much like to see in C++ to support more sophisticated cases, however, is the ability to make code generic over CV-qualifiers. Then you could write something like:
template <cv_qualifier CV, typename A, typename B>
inline CV A &first(CV std::pair<A,B> &p) { return p.first; }
…and have it Just Work.

Date: 2016-06-01 19:02 (UTC)
simont: A picture of me in 2016 (Default)
From: [personal profile] simont
I'm always in two minds about the feature of typeof where it preserves the cv-qualifiers of the thing you apply it to.

For this kind of job, and for other applications involving making a pointer that can address the object you were given, that's exactly what you want. But for another entirely sensible kind of job, it's exactly not what you want – namely, the case in which the whole reason you wanted typeof in the first place was in order to make a mutable copy of the potentially-const object you were given. In that situation, insisting on keeping the const leaves me back where I started! I'd prefer a means of saying which kind of situation is which.

(no subject)

From: [personal profile] gerald_duck - Date: 2016-06-01 20:46 (UTC) - Expand

(no subject)

From: [personal profile] gerald_duck - Date: 2016-06-01 20:42 (UTC) - Expand

(no subject)

From: [personal profile] gerald_duck - Date: 2016-06-01 20:56 (UTC) - Expand

(no subject)

From: [identity profile] caladri.livejournal.com - Date: 2016-06-02 05:39 (UTC) - Expand

Open full-grown galleries

Date: 2016-08-12 09:18 (UTC)
From: (Anonymous)
Free pictures
http://sextoys.porndairy.in/?thalia
korean erotic movie erotic moments erotic jigsaw puzzles mobile erotic free erotic stories

Full-grown galleries

Date: 2016-08-19 15:52 (UTC)
From: (Anonymous)
Disencumber ladyboys
http://shemaledating.sexblog.pw/?page-unique
video shemal.com hard core blackshemale free videos pron.com shemail sex move

Adult galleries

Date: 2016-09-21 04:31 (UTC)
From: (Anonymous)
My contemporary pron blog
http://girlfriend.net.erolove.in/?tweet_kiersten
erotic illustrations korean erotic movies erotic fitness erotic sound
From: (Anonymous)
[color=green][b]Great news everybody![/b][/color]

New updated [b]XRumer 12[/b] recognize and break Google Captcha again,
during automatic registering and posting:

[img]http://s017.radikal.ru/i440/1609/07/3bbec3a7389c.jpg[/img]

Interested? :)

Just Google for the latest subversion of XRumer 12! ;)

[color=gray]P.P.S. XRumer 7.0.12 and another versions are too old - and have less than 10% of functions, that have XRumer 12.[/color]

___
[url=http://www.indieunleashed.co.uk/community/index.php?/topic/95-xrumer-12016-18-google-captcha-solution/]XRumer 12.0.16: Google Captcha - how to solve[/url], [url=http://forum.ebn.ru/viewtopic.php?f=68&t=215919]XRumer 12.0.17: ReCaptcha breaking[/url], [url=http://file.emgu.com/forum/viewtopic.php?f=7&t=9917]XRumer 12.0.17-18: Google Captcha breaking[/url], [url=http://homework.kiev.ua/showthread.php?p=60237#post60237]X-Rumer 12.0.17-18: Google Captcha breaker[/url], [url=http://regalmu.com/forum/viewtopic.php?f=8&t=436989]XRumer 12.0.16-18: ReCaptcha breaking[/url]

Adult position

Date: 2016-10-27 17:00 (UTC)
From: (Anonymous)
Vulgar girls blog
http://condoms.erolove.in/?blog.dayana
erotic ghost stories new erotic erotic photography blog erotic movies online

Latest spot

Date: 2016-10-30 13:46 (UTC)
From: (Anonymous)
New sissy girls blog website
free images free missouri license plate partydress
http://sissyabuse.blogporn.in/?post.regina
xxx boys gay video plastic pants boy halloween fostume ballerina sexy french maid dress ballet dance costume 80s t shirts jane austen biography dad and son gays

Matured purlieus

Date: 2016-11-01 15:36 (UTC)
From: (Anonymous)
Porn gay pit oneself against
http://gay.adultgalls.com/?page.august
famous gays gay history gay hate crimes luther vandross gay gay mobile chat

Matured position

Date: 2016-11-05 09:29 (UTC)
From: (Anonymous)
Pornographic self shots
http://teen.erolove.in/?face-jaiden
best erotic novels erotic hotel massage erotic tv channels erotic coupons

Full-grown galleries

Date: 2016-11-07 00:54 (UTC)
From: (Anonymous)
Modish naked pictures
mallu erotic erotic art books megan fox erotic erotic body video erotic
http://girlfriend.net.erolove.in/?kayla
erotic tv series erotic pants erotic visions pc erotic games erotic museum

Unencumbered galleries

Date: 2016-11-07 15:08 (UTC)
From: (Anonymous)
Pron blog locality
http://glamour.sexblog.pw/?angeline
erotic photoshoots erotic publishers new erotic adultmovies daily erotic picdump

Delivered matured galleries

Date: 2016-11-08 13:45 (UTC)
From: (Anonymous)
Sexy blog pictures from internet
http://hentai.sexblog.pw/?nikki
adult stories sexy hot erotic stories the erotic traveler erotic statue erotic music video

Latest site

Date: 2016-11-10 17:41 (UTC)
From: (Anonymous)
Original programme
http://premium.cars.purplesphere.in/?entry.makenna
free young cartoon porn porn star meridian porn clips of big busted women nina mercedes and porn free brutal xxx porn

Pictures from community networks

Date: 2016-11-11 04:32 (UTC)
From: (Anonymous)
My gay pics
http://blackgay.net.erolove.in/?pg-vincent
gay chicken gay marriage pros gay sites spartacus gay gay fun

Loose adult galleries

Date: 2016-11-12 04:50 (UTC)
From: (Anonymous)
New naked pictures
romance erotic erotic sex erotic words erotic photo shoots erotic trance
http://tranny.net.erolove.in/?maryam
writing erotic literature shop erotic erotic reviews japanese erotic art free sex movies online

Pictures from collective networks

Date: 2016-11-14 16:12 (UTC)
From: (Anonymous)
Pron blog neighbourhood
http://lingerie.sexblog.pw/?xiomara
erotic weight gain erotic comic book erotic ebook erotic heritage museum free erotic wallpapers

Loose galleries

Date: 2016-11-16 00:09 (UTC)
From: (Anonymous)
Stunning shemales
http://futanari.replyme.pw/?blog-marilyn
young shemal pic freeshemale movie shemans sex tgirl p or n sex

Mature galleries

Date: 2016-11-18 01:16 (UTC)
From: (Anonymous)
Daily updated sissy blog
hottest teens porn videos seed bank spain free digital photos
http://sissytales.porndairy.in/?view.tabitha
transgender males man fucks teenager men sexy photo freeporn videp modeling websites sexy in a skirt equality women fuck teens xvideos

Free matured galleries

Date: 2016-11-18 15:48 (UTC)
From: (Anonymous)
My new pron blog
http://busty.net.erolove.in/?tweet_natalia
erotic stock photo erotic romances erotic sound erotic audiobooks

Latest plat

Date: 2016-11-18 15:49 (UTC)
From: (Anonymous)
Free pictures
erotika erotic hindi movies erotic food erotic spa erotic korean
http://latexsex.net.erolove.in/?robyn
indian erotic art erotic hypnotist erotic film list erotic acts erotic kisses

My supplementary website

Date: 2016-11-19 10:14 (UTC)
From: (Anonymous)
Pron blog locality
erotic penpal erotic hug free adult movies erotic romance movies erotic acts
http://tranny.net.erolove.in/?macy
dildo erotic roleplay best erotic game erotД±c storД±es madonna erotic

My unfamiliar website

Date: 2016-11-20 02:22 (UTC)
From: (Anonymous)
New free porn instal
sexy videos erotic strangle sex erotic saloon cosplay erotic
http://sexdating.erolove.in/?reese
erotic movies online erotic radio erotic sound phone erotica erotic thoughts

Grown up purlieus

Date: 2016-11-20 02:53 (UTC)
From: (Anonymous)
Pornographic blog pictures from internet
free adult game erotic sculptures erotic baby erotic rpg free pone xxx
http://selfshots.erolove.in/?paula
pinoy erotic movies erotic art history erotic romance novel erotic sports erotic wallpaper

Public pictures

Date: 2016-11-21 16:43 (UTC)
From: (Anonymous)
New naked pictures
erotic dance erotic cosplay erotic pages erotic hypnosis video erotic vampire books
http://strapon.erolove.in/?aja
erotic india erotic reviews ww.erotic.com japanese erotic prints erotic calendar
Page 1 of 3 << [1] [2] [3] >>

December 2025

S M T W T F S
 123456
78910111213
14151617181920
21222324 252627
28293031   

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated 2026-01-07 15:43
Powered by Dreamwidth Studios