我想创建一个模板化的类,其变量模板参数与给定的std::function
相同。例如
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
template <template<typename... TArgs> class T>
using ArgsCopy = T<Args...>;
};
template <typename... Args>
class _forwarder {
public:
using cb = std::function<void(Args...)>;
void operator()(Args... args) {
my_cb(args...);
}
private:
cb my_cb;
};
template <typename T>
using Forwarder = function_traits<T>::ArgsCopy<_forwarder>;
然后我将使用此类如下
using cb_test = std::function<void(int, float, std::string)>;
Forwarder<cb_test> fwd;
fwd(5, 3.2, "hello");
Visual Studio抛出编译错误:错误C2061:语法错误:标识符'ArgsCopy'。我该如何解决这个问题?
答案 0 :(得分:3)
Microsoft的编译器并不完全遵循此处的标准,添加template
应该有效:
template <typename T>
using Forwarder = function_traits<T>::template ArgsCopy<_forwarder>;
但如果您希望自己的代码可移植,请添加typename
:
template <typename T>
using Forwarder = typename function_traits<T>::template ArgsCopy<_forwarder>;