Re: [HACKERS] static assertions in C++

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Michael Paquier <michael(dot)paquier(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [HACKERS] static assertions in C++
Date: 2017-12-20 05:57:12
Message-ID: 15959.1513749432@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com> writes:
> On 12/11/17 17:12, Tom Lane wrote:
>> Hmm, well, surely there's more than one way to do that; the sizeof
>> is just a convenient way to wrap it in C. Wouldn't a typedef serve
>> just as well?

> Here is another attempt, which has the desired effect with the handful
> of compilers I have available.

I can confirm that the negative-bitfield-width method employed here
has the desired effects (i.e., error or not, without unwanted warnings)
on the oldest C++ compilers I have handy, namely

$ g++ -v
Reading specs from /usr/local/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95.3/specs
gcc version 2.95.3 20010315 (release)

$ g++ -v
Using built-in specs.
Target: powerpc-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5341.obj~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=powerpc-apple-darwin8 --host=powerpc-apple-darwin8 --target=powerpc-apple-darwin8
Thread model: posix
gcc version 4.0.1 (Apple Computer, Inc. build 5341)

I do not have a well-informed opinion on whether

#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410

is an appropriate test for static_assert() being available, but I'm
pretty suspicious of it because none of my C++ compilers seem to
take that path, not even recent stuff like clang 9.0.0. However,
since the negative-bitfield-width code path works anyway, that's
something we could refine later.

In short, I think this could be committed as-is, but later we might want
to do some more research on how to tell whether static_assert() is
available.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ildar Musin 2017-12-20 05:58:18 General purpose hashing func in pgbench
Previous Message Huong Dangminh 2017-12-20 05:28:39 RE: User defined data types in Logical Replication