0

I have a use case where I need to use counting_semaphore as a data member in a class. If it were a global variable, I could've omitted the template argument, and it would've been default initialized. But as mentioned here, in case of a member variable, the template argument needs to be specified, and in our case it has to be a compile-time constant.

So, I'm not sure what to initalize the value of LeastMaxValue to? Are there any heuristics for it that you use, or is there a way I can still use the implementation defined default value for this?

tarun26
  • 23
  • 4

1 Answers1

1

Ideally, you would look at how the data member will be used and determine an upper bound on what the semaphore needs to count. This upper bound is an appropriate LeastMaxValue.

It is not always possible to find such a bound, though. If you have no way of bounding the maximum the data member needs to handle, you could use the largest possible value, std::numeric_limits<std::ptrdiff_t>::max().

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 1
    Are there any drawbacks to initializing it to the max possible value? And if there aren't, why don't we always initialize it to that as the default value? – tarun26 Aug 15 '22 at 07:17
  • @tarun26 The answer to that would be pretty similar to the answer to the following: *Are there any drawbacks to using `long long`? And if there aren't, why don't we always use `long long` (instead of other integer types)?* – JaMiT Aug 16 '22 at 01:48
  • Thanks, that clarifies it. But this means that there's a certain default value already for it, but why can't I access that with some macro, etc. and use it in case I have to use the type within a class? Also, does the value of max() depend on LeastMaxValue that we provide? – tarun26 Aug 16 '22 at 07:04