Dan*_*Dan 1 c++ visual-c++ c++11 visual-studio-2012
隐藏unique_ptr丑陋的以下"技巧"有什么问题?
class Drawable;
typedef unique_ptr<Drawable> pDrawable;
#define newDrawable(...) pDrawable(new Drawable (##__VA_ARGS__))
Run Code Online (Sandbox Code Playgroud)
前两个很好.但第三个是在VS2012中导致错误:
23 IntelliSense: "std::unique_ptr<_Ty, _Dx>::unique_ptr(const std::unique_ptr<_Ty, _Dx>::
_Myt &) [with _Ty=Drawable, _Dx=std::default_delete<Drawable>]" (declared at line 1447 of
"C:\vs2012\VC\include\memory") is inaccessible file.h 36 26
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这不会起作用,除非我误解了C++如何定义宏的工作原理.我以为它只会替换这段代码:
newDrawable(a, b, c)
Run Code Online (Sandbox Code Playgroud)
同
unique_ptr<Drawable>(new Drawable(a, b, c));
Run Code Online (Sandbox Code Playgroud)
我知道unique_ptr无法复制,但我不是在复制它.我呢?
编辑:
我收到了一些关于"使用"宏的请求:
如果我是用它,它会被使用如下:
pDrawable myDraw = newDrawable();
Run Code Online (Sandbox Code Playgroud)
我想翻译成:
unique_ptr<Drawable> myDraw = unique_ptr<Drawable>(new Drawable());
Run Code Online (Sandbox Code Playgroud)
但是,如果没有visual studio给出以下错误,我甚至无法编译宏.就好像#define中的某些内容本身是不允许的.在我进行定义的行上返回错误,而不是在我调用define的地方.
请参阅此处了解make_unique无效的原因:make_unique无法编译
EDIT2
我已经回答了下面的问题.上面的代码确实可以编译和工作.
Omn*_*ous 14
好吧,#define是的,恕我直言,这是一个巨大的问题,因为它不遵守范围规则,他们做简单的文本替换,有时会产生令人惊讶的结果.我认为预处理器宏是我需要完成某项工作的最后手段.
定义一个像make_shared返回的模板会好得多unique_ptr<T>.这是允许的,因为您可以将其移动到位.
auto a_drawable = make_unique<Drawable>(a, b, c);
template <typename T, typename... Args>
::std::unique_ptr<T> make_unique(Args&&... args)
{
return ::std::unique_ptr<T>{new T(::std::forward<Args>(args)...)};
}
Run Code Online (Sandbox Code Playgroud)
这比宏更干净,适用于任何非数组类型.这是一个问题.它也可以用于数组类型,但是代码有点复杂,而不是重复它,我只想指出一个有用的评论者给我的答案:
至于为什么你在使用宏时遇到错误,我不确定.看起来你在某些上下文中使用你的宏,结果变成了a const ::std::unique_ptr<Drawable> &,然后尝试将它构造成一个不同的pDrawable.这不行.为什么会这样,我不知道.它主要取决于您使用宏的上下文,而您没有提供.
这只是突出了我的第一点.宏的一个原因真的很丑,因为它们是简单的文本替换,它们的含义可能会根据上下文而改变.