getOpt with shared

Danny Arends Danny.Arends at gmail.com
Fri May 11 17:36:34 UTC 2018


On Friday, 11 May 2018 at 17:25:44 UTC, Danny Arends wrote:
> Hey all,
>
> I have been working on creating a multi-threaded application, 
> so I have a shared configuration object which hold several 
> command line parameters (which I fill using getopt).
>
> The problem is that I get deprecation warnings when trying to 
> set numerical values:
>
> /usr/include/dmd/phobos/std/getopt.d(895,36): Deprecation: 
> read-modify-write operations are not allowed for shared 
> variables. Use core.atomic.atomicOp!"+="(*receiver, 1) instead.
>
> using getopt with a shared object and boolean values seems 
> completely broken:
>
> /usr/include/dmd/phobos/std/getopt.d(895,34): Error: operation 
> not allowed on bool *receiver += 1
> /usr/include/dmd/phobos/std/getopt.d(751,46): Error: template 
> instance `std.getopt.handleOption!(shared(bool)*)` error 
> instantiating
> /usr/include/dmd/phobos/std/getopt.d(435,15):        6 
> recursive instantiations from here: getoptImpl!(string, 
> shared(string)*, string, shared(string)*, string, 
> shared(VSync)*, string, shared(ulong)*, string, shared(bool)*, 
> string, shared(Verbose)*)
>
> Is getopt not supposed to be used with shared structs ?

small example:

import std.getopt : getopt;

struct Settings {
   size_t myvalue = 0;
}

shared(Settings) config;

int main(string[] args) {
   getopt(args, "m|myvalue", &(config.myvalue));
   return(0);
}

Changing size_t to bool, results in the compilation error 
mentioned in the previous post


More information about the Digitalmars-d-learn mailing list