【发布时间】:2014-06-08 02:26:40
【问题描述】:
我正在使用 GCC 4.8 编译以下代码:
#include <memory>
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args&& ...args) {
return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
}
struct S {
template<class... Args>
static std::unique_ptr<S> create(Args&&... args) {
return make_unique<S>(std::forward<Args>(args)...);
}
private: // if I remove this line, then the compilation is OK
S(int) {}
S() = default;
};
int main() {
auto s1 = S::create(); // OK
auto s2 = S::create(0); // Compilation error
}
谁能解释一下编译器出现这个错误的原因?
main.cpp: 在 'std::unique_ptr make_unique(Args&& ...) [与 T = S; Args = {int}]':
main.cpp:11:58: 来自'static std::unique_ptr S::create(Args&& ...) [with Args = {int}]'
main.cpp:20:26:从这里需要
main.cpp:14:5: 错误:'S::S(int)' 是私有的
S(int) {} ^main.cpp:5:65: 错误:在此上下文中 return std::unique_ptr(new T{std::forward(args)...});
^
【问题讨论】: