C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Second draft of a __COUNTER__ proposal

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Tue, 27 Aug 2024 09:15:27 -0500
Thanks for elaborating, I see what you mean. I don’t know why __COUNTER__
has become existing practice over an approach like this, other that
__COUNTER__ working before C++14 and perhaps being more straightforward in
some regards. A consteval counter is an interesting idea but I also don’t
know how difficult that would be to add.

On Tue, Aug 27, 2024 at 00:43 Robin Savonen Söderholm <
robinsavonensoderholm_at_[hidden]> wrote:

> It depends on the scope that the NEW_VAR is intended to work at (function
> scope or namespace scope). At namespace scope you could do:
> ```c++
> template <int>
> auto _benchmarks = 0;
> #define BENCHMARK(fn) \
> auto _benchmarks<counter()> =
> (::benchmark::internal::RegisterBenchmarkInternal(#fn, fn));
>
> ```
>
> Something similar could probably be done in function scope, or by using
> regular function calls instead.
>
> On Tue, Aug 27, 2024, 04:17 Jeremy Rifkin via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> The first approach I tried was what Thiago suggested. The second was:
>>
>> // t.cpp
>> __COUNTER__
>> #include "t.hpp"
>>
>> // t.hpp
>> #if __COUNTER__ != 0
>> #include __FILE__
>> #endif
>>
>>
>> Jeremy
>>
>> On Mon, Aug 26, 2024 at 5:22 PM Arthur O'Dwyer
>> <arthur.j.odwyer_at_[hidden]> wrote:
>> >
>> > On Mon, Aug 26, 2024 at 3:16 PM Jeremy Rifkin via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>> >>
>> >> Good idea. I gave that a try as well as another approach but was
>> >> unable to produce a sufficiently large __COUNTER__ before gcc
>> >> segfaulted after (around a count of 270 million). I'm impartial on
>> >> specifying a 32-bit counter vs a 64-bit counter, both seem reasonable
>> >> to me.
>> >
>> >
>> > Could you share your approach?
>> > For comparison, I used this Python script to generate 50 million
>> __COUNTER__ invocations:
>> >
>> > cat >test.py <<EOF
>> > import sys
>> > print("""
>> > #define C __COUNTER__
>> > #define D C|C|C|C|C|C|C|C|C|C
>> > #define S static_assert(D|D|D|D|D|D|D|D|D|D)
>> > #define T S;S;S;S;S;S;S;S;S;S
>> > #define U T;T;T;T;T;T;T;T;T;T
>> > """)
>> > for i in range(int(sys.argv[1])):
>> > print("U;")
>> > print("")
>> > EOF
>> > time python test.py 5000 >test.cpp ; time clang++ test.cpp
>> -fsyntax-only -std=c++17
>> >
>> > This compilation takes about 30 seconds on my machine.
>> > But AppleClang starts ICEing ("translation unit is too large for Clang
>> to process: ran out of source locations") somewhere between 50 million and
>> 100 million, so that didn't tell me anything about the practical behavior
>> of its __COUNTER__ on overflow.
>> >
>> > FWIW, looking at the actual code of each compiler, it's easy to tell
>> that...
>> > - Clang uses an `unsigned` 32-bit __COUNTER__, and wraps to zero on
>> overflow.
>> > - GCC uses an `unsigned int` 32-bit __COUNTER__, and wraps to zero on
>> overflow.
>> >
>> > Cheers,
>> > Arthur
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>

Received on 2024-08-27 14:15:40