不能为foo
和foo2
推断出模板模板参数。
如果我删除了sfinae部分,则成功推导出foo
和foo2
的模板模板参数。
如何修复span
班级或foo
和foo2
?
感谢。
测试(也在godbolt.org)
#include <type_traits>
template<typename T>
using enable_if_t_const = typename std::enable_if<
std::is_const<T>::value
>::type;
template<typename T, typename=void> class span;
template<typename T>
class span<T, enable_if_t_const<T>> {
public:
explicit span(const T* const data) {}
};
template <typename T, template<typename> class S>
void foo() {}
template <typename T, template<typename> class S>
void foo2(S<T>& s) {}
int main() {
int arr[] = {1};
span<const int> s(arr);
foo<const int, span>();
foo2(s);
return 0;
}
答案 0 :(得分:4)
这是因为,虽然您有默认模板参数,但span
不是template <typename> class S
。这是一个template <typename, typename> class S
。
最简单的解决方法是将其更改为
template <typename T, template<typename...> class S>
void foo2(S<T>& s) {}
这样你就可以接受任何类型的S
(虽然我们只用一个)。