[cfe-dev] Use of deleted function std::unique_ptr

David Blaikie via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 15 10:53:24 PDT 2017


You can still use std::copy, but you'd need to pass Toks.get() as the last
arg, instead of just 'Toks'.

On Wed, Jun 14, 2017 at 4:04 PM Masoud Ataei via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hello Chenwj,
>
> Thank you for your reply. I did the following and it did work:
>
> replace:
> ###
> Token *Toks = new Token[Pragma.size()];
> std::copy(Pragma.begin(), Pragma.end(), Toks);
> PP.EnterTokenStream(Toks, Pragma.size(), /*DisableMacroExpansion=*/true,
> /*OwnsTokens=*/true);
> ##
> by:
> ##
> auto Toks = llvm::make_unique<Token[]>(Pragma.size());
> Toks[0] = Pragma[0];Toks[1] = Pragma[1];Toks[2] = Pragma[2];Toks[3] =
> Pragma[3];
> PP.EnterTokenStream(std::move(Toks), Pragma.size(), true);
> ##
>
> Bests
>
> On Wed, Jun 14, 2017 at 6:20 PM, 陳韋任 <chenwj.cs97g at g2.nctu.edu.tw> wrote:
>
>> Probably you can `grep -r "EnterTokenStream" lib/*` to see how pass
>> unique_ptr to
>> EnterTokenStream. Seem you need to use std::move.
>>
>> HTH,
>> chenwj
>>
>>
>> 2017-06-15 0:43 GMT+08:00 Masoud Ataei via cfe-dev <
>> cfe-dev at lists.llvm.org>:
>>
>>> Hello all,
>>>
>>> I have the following lines of code. To run it, I need to change the rule
>>> of the function "EnterTokenStream" from private to a public function
>>> in "/usr/local/include/clang/Lex/Preprocessor.h". In this case this code
>>> will working with raw pointer "*Toks".
>>> ------------------------------------
>>> PP.Lex(Tok);
>>> SmallVector<Token, 4> Pragma;
>>> Pragma.push_back(Tok);
>>>
>>> Token *Toks = new Token[Pragma.size()];
>>> std::copy(Pragma.begin(), Pragma.end(), Toks);
>>> PP.EnterTokenStream(Toks, Pragma.size(), true, true);
>>> --------------------------------------------------------------
>>>
>>> But default rule for the function "EnterTokenStream" is private. So I
>>> cannot call this function, and I should call this one:
>>> ---------------------------------------------
>>> void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
>>> bool DisableMacroExpansion)
>>> ---------------------------------------------
>>>
>>>
>>> First argument of this function needs  "std::unique_ptr<Token[]> Toks".
>>> So I have tried to define Toks as a unique pointer. I did like this:
>>> --------------------------------------------
>>> PP.Lex(Tok);
>>> SmallVector<Token, 4> Pragma;
>>> Pragma.push_back(Tok);
>>>
>>> unique_ptr<Token[]> Toks(new Token[Pragma.size()]);
>>> std::copy(Pragma.begin(), Pragma.end(), Toks);
>>> PP.EnterTokenStream(Toks, Pragma.size(), true);
>>> ------------------------------------------------
>>>
>>> but it gives me the following error for std::copy:
>>> ---------------------------------------------
>>>  error: use of deleted function ‘std::unique_ptr<_Tp [],
>>> _Dp>::unique_ptr(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp =
>>> clang::Token; _Dp = std::default_delete<clang::Token []>]’
>>>     std::copy(Pragma.begin(), Pragma.end(), Toks);
>>> ----------------------------------------------------------
>>>
>>> and the same error for EnterTokenStream:
>>> ------------------------------------------------------
>>> error: use of deleted function ‘std::unique_ptr<_Tp [],
>>> _Dp>::unique_ptr(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp =
>>> clang::Token; _Dp = std::default_delete<clang::Token []>]’
>>>     PP.EnterTokenStream(Toks, Pragma.size(), true);
>>> ------------------------------------------------------------
>>>
>>> Can anyone help me how to fix this errors? How can I change my original
>>> code to something that I can use the new EnterTokenStream function?
>>>
>>> Thank you.
>>>
>>> --
>>> ------
>>> ----------
>>> Masoud Ataei (Mr.)
>>>
>>>
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>>
>>>
>>
>>
>> --
>> Wei-Ren Chen (陳韋任)
>> Homepage: https://people.cs.nctu.edu.tw/~chenwj
>>
>
>
>
> --
> ------
> ----------
> Masoud Ataei (Mr.)
> http://publish.uwo.ca/~mataeija
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170615/32516af5/attachment.html>


More information about the cfe-dev mailing list