Using std.stdio.File in a class

Spacen Jasset spacenjasset at mailrazer.com
Tue Apr 8 08:54:45 PDT 2014


On Tuesday, 8 April 2014 at 15:18:20 UTC, Steven Schveighoffer 
wrote:
> On Tue, 08 Apr 2014 10:52:01 -0400, Spacen Jasset 
> <spacenjasset at mailrazer.com> wrote:
>
>> I am trying to use file in a class. It doesn't seem to work. 
>> File is a struct. Are you not supposed to new structs anymore? 
>> The examples seem to use auto, but that isn't going to work in 
>> this situation.
>>
>> Is File a struct to take advantage of Raii?
>>
>> import std.stdio;
>>
>> class X
>> {
>>     this()
>>     {
>>         f = new File("test2", "wt");
>>     }
>>
>>     File f;
>> }
>>
>>
>> void main()
>> {
>>     X x = new X;
>> }
>>
>> C:\temp>dmd file.d && file.exe
>> file.d(7): Error: constructor std.stdio.File.this 
>> (shared(_iobuf)* handle, strin
>> g name, uint refs = 1u, bool isPopened = false) is not 
>> callable using argument t
>> ypes (File*)
>>
>> DMD32 D Compiler v2.065
>
> Just a word of caution, I don't think the RAII semantics of 
> File work properly in multithreaded code when the File is 
> destroyed by the GC.
>
> -Steve

What do you mean by don't work? I can see that it won't be RAII 
anymore since there is no stack scope. However, I expect it to 
get closed properly - as and when the containing object is 
collected, or when you use the scope keyword with a class object.

Is this not so?



More information about the Digitalmars-d-learn mailing list