在c++中有使用模板化函数的方法吗?
Is there a way to do this using templated functions in c++
我目前正在编写一些将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中,我们可以通过auto
和decltype
来改进这一点。下面是修改后的示例:
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操作符[]的返回类型
- 如何在 c++ 的类中递归调用函数方法?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 指向类中函数方法的指针不起作用
- 如何发送通过绑定到函数/方法创建的函数对象?
- 编译错误,未创建函数/方法! 对于 brms 模型
- 对静态重载(类)函数/方法的调用是不明确的
- 复制构造函数方法的用法
- C/C++ 函数/方法与 Java 的公开
- std ::函数方法参考无法将dword投入到dword64
- 链表的析构函数方法
- 为什么在类构造函数方法中,std::string 参数在调试时显示不同的结果?
- 我的构造函数方法不接受参数(DirectX / Windows)
- 当函数/方法应该要求 * 或 &
- 带有常量构造函数参数的C++变量构造函数方法
- 使用模板递归检查函数方法是否存在
- 如何通过析构函数方法删除对象(类)
- 为什么在 Java 和 C++ 中不允许隐藏虚拟函数/方法?
- 在构造函数方法中返回一个子类