我可以使用gcc 6.5编译以下代码。但是,它无法通过gcc 5.5的编译。谁能给我一些解决方法?
#include <string>
#include <memory>
#include <type_traits>
#include <functional>
#include <utility>
#include <iostream>
#include <experimental/optional>
#include <tuple>
template<typename T>
class Future;
template<typename T, template<typename> class FutureType = Future>
std::shared_ptr<FutureType<T>> MakeReadyFuture(T&& v);
template<typename T>
class Future {
protected:
template<typename O, template<typename> class FutureType>
std::shared_ptr<FutureType<O>> friend MakeReadyFuture(O&& v);
Future(T& v) {}
};
template<typename T, template<typename> class FutureType>
std::shared_ptr<FutureType<T>> MakeReadyFuture(T&& v) {
return std::shared_ptr<FutureType<T>>(new FutureType<T>(v));
}
int main() {
MakeReadyFuture(1);
return 0;
}
错误是:
21:5:错误:'Future :: Future(T&)[with T = int]'受保护
可以在https://godbolt.org/z/ZXlHK1上进行测试
答案 0 :(得分:0)
看起来像是GCC的错误。这是我在GCC 5.5上编译的解决方法:
#include <string>
#include <memory>
#include <type_traits>
#include <functional>
#include <utility>
#include <iostream>
#include <experimental/optional>
#include <tuple>
template<typename T>
class Future;
struct FutureCreator{
template<typename T, template<typename> class FutureType = Future>
static std::shared_ptr<FutureType<T>> MakeReadyFuture(T&& v) {
return std::shared_ptr<FutureType<T>>(new FutureType<T>(v));
}
};
template<typename T>
class Future {
protected:
friend FutureCreator;
Future(T& v) {}
};
int main() {
FutureCreator::MakeReadyFuture(1);
return 0;
}