C++/CLI堆栈语义相当于C#的现有对象使用语句?

Mir*_*ral 6 .net idisposable using c++-cli finally

我知道C++/CLI相当于这个C#代码:

using (SomeClass x = new SomeClass(foo))
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

这是:

{
    SomeClass x(foo);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但有没有类似简洁和类似RAII的方式来表达这一点:

using (SomeClass x = SomeFunctionThatReturnsThat(foo))
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

要么:

SomeClass x = SomeFunctionThatReturnsThat(foo);
using (x)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

?我最接近的工作示例是:

SomeClass^ x = SomeFunctionThatReturnsThat(foo);
try
{
    // ...
}
finally
{
    if (x != nullptr) { delete x; }
}
Run Code Online (Sandbox Code Playgroud)

但这似乎不太好.

ild*_*arn 10

msclr::auto_handle<> 是托管类型的智能指针:

#include <msclr/auto_handle.h>

{
    msclr::auto_handle<SomeClass> x(SomeFunctionThatReturnsThat(foo));
    // ...
}

// or

SomeClass^ x = SomeFunctionThatReturnsThat(foo);
{
    msclr::auto_handle<SomeClass> y(x);
    // ...
}
Run Code Online (Sandbox Code Playgroud)