我使用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: In instantiation of ‘std::unique_ptr make_unique(Args&&
…) [with T = S; Args = {int}]’:main.cpp:11:58: required from ‘static std::unique_ptr
S::create(Args&& …) [with Args = {int}]’main.cpp:20:26: required from here
main.cpp:14:5: error: ‘S::S(int)’ is private
06001
main.cpp:5:65: error: within this context
return std::unique_ptr(new T{std::forward(args)…});06002