模板模板参数演绎与sfinae

时间:2018-04-26 20:17:00

标签: c++ templates sfinae template-templates

不能为foofoo2推断出模板模板参数。

如果我删除了sfinae部分,则成功推导出foofoo2的模板模板参数。

如何修复span班级或foofoo2

感谢。

测试(也在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;
}

1 个答案:

答案 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(虽然我们只用一个)。

Demo