具有多层类型测定的SFINAE

SFINAE with multi-layer type determination

本文关键字:SFINAE 有多层 类型      更新时间:2023-10-16

下面的代码专门化了 f() 的两个版本。第一个检测向量并返回迭代器。第二个采用所有其他类型并返回副本。

这无法在 VC 2010 上编译,并在 GetIter2 中出现错误,即 GetIter::type 不存在。这仅在使用非向量解析对 f() 的调用时。如果我删除一层间接类型,使用 GetIter 而不是 GetIter2(请参阅注释行)作为返回类型,那么一切正常。

我想我想知道这是编译器错误还是正确的行为。如果这看起来很奇怪,那是因为它减少了我使用 boost::range_iterator 遇到的问题,我不能只是删除 GetIter2 代表的内容。

#include <vector>
using namespace std;
template<typename T>
struct GetIter {
};
template<typename T>
struct GetIter<vector<T>> {
    typedef typename vector<T>::iterator type;
};
template<typename T>
struct GetIter2
{
    typedef typename GetIter<T>::type type;
};
template<typename T>
typename enable_if<is_same<T, vector<int>>::value, typename GetIter2<T>::type>::type
//typename enable_if<is_same<T, vector<int>>::value, typename GetIter<T>::type>::type
f(T & t) {
    return t.begin();
}
template<typename T>
typename enable_if<!is_same<T, vector<int>>::value, T>::type
f(T & t) {
    return t;
}
int main(int argc, char* argv[])
{
    vector<int> v(2);
    int i = 6;
    f(v);
    f(i);  // error C2039: 'type' : is not a member of 'GetIter<T>'
    return 0;
}

编辑:这是我试图解决的实际问题。第二次调用 copy() 时,迭代器作为第二个参数,在 boost::mpl::eval_if_c 对象上会导致与上述类似的错误。

#include <vector>
using namespace std;
#include <boost/range.hpp>
#include <boost/tti/has_type.hpp>
BOOST_TTI_TRAIT_HAS_TYPE(has_iterator, iterator)
template<typename InCont, typename Out>
typename enable_if<has_iterator<Out>::value, typename boost::range_iterator<Out>::type>::type
copy(InCont const & in_cont, Out & out_cont)
{
    return std::copy(boost::begin(in_cont), boost::end(in_cont), boost::begin(out_cont));
}
template<typename InCont, typename Out>
typename enable_if<!has_iterator<Out>::value, Out>::type
copy(InCont const & in_cont, Out & out_iter)
{
    return std::copy(boost::begin(in_cont), boost::end(in_cont), out_iter);
}
int main(int argc, char* argv[])
{
    vector<int> v1;
    vector<int> v2;
    copy(v1, v2);
    copy(v1, v2.begin());  // error C2039: 'type' : is not a member of 'boost::mpl::eval_if_c<C,F1,F2>'
    return 0;
}

编辑2:原始问题已在最新版本的 boost::range_iterator 中修复。一旦我修补了,事情就变得更容易了。这是我登陆的内容,使用 boost::has_range_iterator 检查容器:

#include <vector>
using namespace std;
#include <boost/range.hpp>
template<typename InCont, typename Out>
typename boost::range_iterator<Out>::type
copy(InCont const & in_cont, Out & out_cont)
{
    return std::copy(boost::begin(in_cont), boost::end(in_cont), boost::begin(out_cont));
}
template<typename InCont, typename Out>
typename enable_if<!boost::has_range_iterator<Out>::value, Out>::type
copy(InCont const & in_cont, Out out_iter)
{
    return std::copy(boost::begin(in_cont), boost::end(in_cont), out_iter);
}
int main(int argc, char* argv[])
{
    vector<int> v1;
    vector<int> v2;
    copy(v1, v2);
    copy(v1, v2.begin());
    return 0;
}

我仍在尝试找到标准的相关部分来解释为什么这不起作用。我认为这与替换GetIter2<T>没有失败但随后访问GetIter2<T>::type有关,然后确定GetIter<T>::type不存在。通过使GetIter2<T>模板无法立即替换,您不会收到错误。您可以通过稍微更改其定义来做到这一点:

template<typename T, typename ST = typename GetIter<T>::type>
struct GetIter2
{
    typedef ST type;
};

我能够让你的真实示例工作,而无需使用以下代码修改提升类型:

template<typename InCont, typename Out, typename = typename enable_if<has_iterator<Out>::value>::type>
typename boost::range_iterator<Out>::type
copy(InCont const & in_cont, Out & out_cont)
{
    return std::copy(boost::begin(in_cont), boost::end(in_cont), boost::begin(out_cont));
}

为什么它不起作用

规则是,从[温度扣除],强调我的:

只有函数类型及其模板参数类型的直接上下文中的无效类型和表达式才可能导致推导失败。

直接上下文是您在声明本身中看到的内容。尝试替换T=int时发生的故障不在typename GetIter2<T>::type的直接上下文中 - 当尝试确定该类型实际是什么并看到GetIter<int>没有type成员时,就会发生故障。由于这不是直接上下文,因此这不是扣除失败 - 这是一个硬错误。

注意:即使这是扣除失败,它仍然仅适用于std::vector<int, std::allocator<int>>,而不适用于任何向量。

如何使其工作

实际上,您在这里根本不需要SFINAE。只需对vector进行一次重载,对所有内容进行另一次重载:

template <class T, class A>
auto f(std::vector<T,A>& t) {
    return t.begin();
}
template <class T>
T f(T& t) {
    return t;
}

我认为非矢量 GetIter 错过了一个类型函数。 模板 struct GetIter { 类型定义 T 型; };