make_unique不编译 [英] make_unique does not compile

查看:271
本文介绍了make_unique不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建和使用 make_unique std :: unique_ptr c $ c> std :: make_shared 存在 std :: shared_ptr 。 Herb Sutter 提及 make_unique 的可能实现,如下所示:

I'm trying to create and use make_unique for std::unique_ptr, in the same way std::make_shared exists for std::shared_ptr described here. Herb Sutter mentions the possible implementation of make_unique that looks like this:

template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

似乎不适合我。我使用下面的示例程序:

It doesn't seem to work for me. I'm using the following sample program:

// testproject.cpp : Defines the entry point for the console application.
#include "stdafx.h"

#include <iostream>
#include <memory>
#include <utility>

struct A {
  A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; }
  A(int& n)  { std::cout << "lvalue overload, n=" << n << "\n"; }
};

template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args ) {
  return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

int main() {
  std::unique_ptr<A> p1 = make_unique<A>(2); // rvalue
  int i = 1;
  std::unique_ptr<A> p2 = make_unique<A>(i); // lvalue
}

编译器(我使用VS2010)以下输出:

And the compiler (I'm using VS2010) gives me the following output:

1>d:\projects\testproject\testproject\testproject.cpp(15): error C2143: syntax error : missing ',' before '...'
1>d:\projects\testproject\testproject\testproject.cpp(16): error C2065: 'Args' : undeclared identifier
1>d:\projects\testproject\testproject\testproject.cpp(16): error C2988: unrecognizable template declaration/definition
1>d:\projects\testproject\testproject\testproject.cpp(16): error C2059: syntax error : '...'
1>d:\projects\testproject\testproject\testproject.cpp(22): error C2143: syntax error : missing ';' before '{'
1>d:\projects\testproject\testproject\testproject.cpp(22): error C2447: '{' : missing function header (old-style formal list?)

此外,如果替换 make_unique 执行到以下

Also if you replace the make_unique implementation to the following

template<class T, class U>
std::unique_ptr<T> make_unique(U&& u) {
  return std::unique_ptr<T>(new T(std::forward<U>(u)));
}

(取自这个例子),它编译和工作正常。

(which is taken from this example), it compiles and works fine.

任何人都可以告诉我什么问题?在我看来,VS2010在模板声明中有一些麻烦, ... ,我不知道该怎么办。

Can anyone tell me what's the problem? It seems to me that VS2010 is having some trouble with ... in template declaration, and I don't know what can I do about it.

推荐答案

变体模板在Visual C ++ 11的发布版本中不可用。但是,您可以模拟参数扩展与大量的复制/粘贴代码用于不同数量的参数,或者使用相同的编译技巧用于微软自己实现的伪变量。根据Herb Sutter的博客的评论: http://herbsutter.com/gotw/_102/#comment-6428

Variadic templates aren't available in the released version of Visual C++ 11. You can, however, simulate the argument expansion with either lots of copy/paste code for different number of parameters, or use the same compiler tricks used in Microsoft's own implementation of "pseudo-variadics". From this comment on Herb Sutter's blog: http://herbsutter.com/gotw/_102/#comment-6428

#include <memory> // brings in TEMPLATE macros.

#define _MAKE_UNIQUE(TEMPLATE_LIST, PADDING_LIST, LIST, COMMA, X1, X2, X3, X4)  \
\
template<class T COMMA LIST(_CLASS_TYPE)>    \
inline std::unique_ptr<T> make_unique(LIST(_TYPE_REFREF_ARG))    \
{    \
    return std::unique_ptr<T>(new T(LIST(_FORWARD_ARG)));    \
}

_VARIADIC_EXPAND_0X(_MAKE_UNIQUE, , , , )
#undef _MAKE_UNIQUE

这篇关于make_unique不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆