Date: Wed, 15 Nov 2023 19:39:45 +0400
To not create ODR problems you can use macro instead with __LINE__ /
__COUNTER__
ср, 15 нояб. 2023 г. в 16:09, Pavel Vazharov via Std-Proposals <
std-proposals_at_[hidden]>:
> Let's say we have a function as follows:
>>
>> void Func(void)
>> {
>> static int one_for_each_callsite = 0;
>> }
>>
>> We want this function to have a unique static-duration variable for
>> each place in the code that this function is called. It would be cool
>> if we could just write the following:
>>
>> template<callsite>
>> void Func(void)
>> {
>> static int one_for_each_callsite = 0;
>> }
>>
>> And so then we could call this function from a few different places:
>>
>> int main(void)
>> {
>> Func(); // This is the 1st call site
>>
>> while ( Something() ) Func(); // This is the 2nd call site
>>
>> Func(); // This is the 3rd call site
>> }
>>
>> So the above code would have three copies of the variable
>> "one_for_each_callsite".
>>
>> If I understood you correctly, you can achieve this today pretty easily
>
> #include <cstdio>
>
> template <typename Tag = decltype([]{})>
> void fun()
> {
> static int counter = 0;
> std::printf("addr:%p value:%i\n", &counter, counter);
> }
>
> int main()
> {
> fun();
> fun();
> fun();
>
> return 0;
> }
>
> This prints something like <https://godbolt.org/z/9Mh8Ea6P8>
>
> addr:0x40401c value:0
> addr:0x404020 value:0
> addr:0x404024 value:0
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
__COUNTER__
ср, 15 нояб. 2023 г. в 16:09, Pavel Vazharov via Std-Proposals <
std-proposals_at_[hidden]>:
> Let's say we have a function as follows:
>>
>> void Func(void)
>> {
>> static int one_for_each_callsite = 0;
>> }
>>
>> We want this function to have a unique static-duration variable for
>> each place in the code that this function is called. It would be cool
>> if we could just write the following:
>>
>> template<callsite>
>> void Func(void)
>> {
>> static int one_for_each_callsite = 0;
>> }
>>
>> And so then we could call this function from a few different places:
>>
>> int main(void)
>> {
>> Func(); // This is the 1st call site
>>
>> while ( Something() ) Func(); // This is the 2nd call site
>>
>> Func(); // This is the 3rd call site
>> }
>>
>> So the above code would have three copies of the variable
>> "one_for_each_callsite".
>>
>> If I understood you correctly, you can achieve this today pretty easily
>
> #include <cstdio>
>
> template <typename Tag = decltype([]{})>
> void fun()
> {
> static int counter = 0;
> std::printf("addr:%p value:%i\n", &counter, counter);
> }
>
> int main()
> {
> fun();
> fun();
> fun();
>
> return 0;
> }
>
> This prints something like <https://godbolt.org/z/9Mh8Ea6P8>
>
> addr:0x40401c value:0
> addr:0x404020 value:0
> addr:0x404024 value:0
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2023-11-15 15:39:58