Mar*_*tin 9 c++ templates c++11
我正在使用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
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以从编译器解释这个错误的原因吗?
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)'是私有的
Run Code Online (Sandbox Code Playgroud)S(int) {} ^main.cpp:5:65:error:在这个上下文中返回std :: unique_ptr(new T {std :: forward(args)...});
Run Code Online (Sandbox Code Playgroud)^
| 归档时间: |
|
| 查看次数: |
945 次 |
| 最近记录: |