[cfe-dev] Small C++ query

Keno Fischer kfischer at college.harvard.edu
Mon May 25 21:34:45 PDT 2015


Thank you everyone.

I knew about this difficulty in parsing, but did not realize it applied to
this situation. Makes sense. Thanks!

On Tue, May 26, 2015 at 12:17 AM, Gabriel Dos Reis <
gdr at integrable-solutions.net> wrote:

>
>
> On Monday, May 25, 2015, Keno Fischer <kfischer at college.harvard.edu>
> wrote:
>
>> Hi folks,
>>
>> I'm confused by the following code snippet:
>>
>> #include <memory>
>> void foo(int *x) { std::unique_ptr<int>(x); }
>>
>> both clang and gcc warn that this redefines x, but I don't understand why
>> this
>> is a declaration of x. I guess I would have expected a temporary
>> unique_ptr that gets destructed immediately or an error. How come this
>> actually defines a variable called x?
>>
>
> The C and C++ grammars allow redundant parenthesis around names being
> declared (technically, declarators).
> If you wish to construct a temporary, use the brace notation, aka uniform
> initialization syntax.
>
>
>
>>
>> To give some background about where this came up, a user wanted to do
>> C++> std::unique_ptr<int>(x);
>> in my Clang-based interactive C++ REPL. Behind the scenes this gets
>> transformed into the function above and then I try to insert a return
>> statement on the last statement in the body to automatically grab the
>> result. This works well in most cases but failed here because Clang thinks
>> this is a variable declaration, so I'd like to understand why this is
>> happening.
>>
>> Keno
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150526/19440c9a/attachment.html>


More information about the cfe-dev mailing list