在c++中有使用模板化函数的方法吗?

Is there a way to do this using templated functions in c++

本文关键字:函数 方法 c++      更新时间:2023-10-16

我目前正在编写一些将java代码转换为c++代码的代码,结果出现了一些相当棘手的问题。我的问题是,是否有可能有一个重载操作符,从包含类返回模板化的值?

Ie:我希望能够对以下类做以下操作。

SmartPointer<ArrayClass<bool>*> boolArray = new ArrayClass<bool>(true, true, false, false);
bool b = boolArray[1];

template <typename T> class SmartPointer
{
    T data;
    template <typename U>
    U operator [](int i) const
    {
        return ((*T)(*data))[index];
    }
}
template ArrayClass<U>
{
    // Various constructors...
    U operator [](int i) const
    {
        // Implementation here
    }
}

我得到的问题(可以理解)是:操作符const无法推导出U的模板参数编译器不知道U是什么,我想告诉它它是bool值,因为这是ArrayClass将返回的值。SmartPointer可能不包含数组,在这种情况下[]操作符就没有意义了。然而,我希望能够将它传递给智能指针内部的对象,以防万一……

?

我不知道该怎么做才行。也许这是不可能的?

答:

感谢大家的回复。这里提供了3种基本相同的解决方案,但我将此奖励给Oktalist,因为他是第一名。我仍然有一个困难的解决方案,虽然,因为我传递指针到我的SmartPointer类,以允许我使用前向声明的类。这使我无法使用T::value_type作为返回类型,但这似乎是正确的方法。看起来我要求很多编译器,看起来我将不得不恢复到简单地解引用智能指针,以便进行数组访问!

传统的c++ 03方法是使用typedef,通常命名为value_type。在c++ 11中,我们可以通过autodecltype来改进这一点。下面是修改后的示例:

SmartPointerCPP03<ArrayClass<bool>> boolArray = new ArrayClass<bool>(true, true, false, false);
SmartPointerCPP11<ArrayClass<bool>> boolArray = new ArrayClass<bool>(true, true, false, false);
bool b = boolArray[1];
template <typename T> class SmartPointerCPP03
{
    T* data;
    typename T::value_type operator [](int i) const
    {
        return (*data)[i];
    }
}
template <typename T> class SmartPointerCPP11
{
    T* data;
    auto operator [](int i) const -> decltype(std::declval<T>()[i])
    {
        return (*data)[i];
    }
}
template <typename T> class SmartPointerCPP14
{
    T* data;
    auto operator [](int i) const
    {
        return (*data)[i];
    }
}
template <typename U> ArrayClass
{
    // Various constructors...
    typedef U value_type;
    U operator [](int i) const
    {
        // Implementation here
    }
}

我还擅自将T data更改为T* data,并从实例化中的参数中删除*。顺便说一下,你的(T*) cast是错误的,我也删除了。

首先,让SmartPointer接受非指针类型:

SmartPointer<ArrayClass<bool> > boolArray = new ArrayClass<bool>(true, true, false, false);

给ArrayClass添加一个typedef:

template <typename U> class ArrayClass
{
    typedef U value_type;
    ...
};

然后编写一个元函数来获取类型:

template <typename T> struct ValueTypeOf {
    typedef typename T::value_type type;
};

然后在SmartPointer中使用:

template <typename T> 
class SmartPointer
{
    typedef typename ValueTypeOf<T>::type value_type;
    T* data;
    value_type operator [](int i) const
    {
        return ((*data))[index];
    }
};

通过使用value_typeof元函数,您可以根据类型对其进行专门化,因此,如果您的类型没有value_type成员,您可以做一些不同的事情来获取它。

编辑:为指针类型专门化示例:

struct A {
    typedef int value_type;
};
template <typename T>
struct ValueTypeOf
{
    typedef typename T::value_type type;
};
template <typename T>
struct ValueTypeOf<T*>
{
    typedef typename T::value_type type;
};

int main()
{
    ValueTypeOf<A>::type  foo = 0; // foo is an int
    ValueTypeOf<A*>::type bar = 0; // bar is an int
    return 0;
}

已经有一段时间了,但我以前经常这样做。下面的代码应该可以工作:

在ArrayClass中定义一个名为value_type的typedef,并将typedef U赋给它。然后使用T::value_type作为SmartPointer操作符[]的返回类型