如何在新模板中使用模板化类?
How do I use a templated class within a new template?
本文关键字:新模板 更新时间:2023-10-16
不知道为什么,但是xcode中的代码刚刚开始工作。 我很高兴。 谢谢大家抽出宝贵时间。
//here is JUCE's code
//==============================================================================
/** This template-overloaded class can be used to convert between var and custom types.
@tags{Core}
*/
template <class Type> struct VariantConverter
{
static Type fromVar (const var& v) { return static_cast<Type> (v); }
static var toVar (const Type& t) { return t; }
};
//here is my addition
template <> struct VariantConverter<Point<int>>
{
static Point<int> fromVar (const var& v)
{
return Point<int>
(
{ v[0].operator int(), v[1].operator int() }
);
}
static var toVar (const Point<int>& p)
{
return Array<var>({ p.getX(), p.getY() });
}
};
嗨,我不确定如何实现上述代码。 像 typedef 这样的类来替换
Point<int>
会很棒。 我正在附加一个 JUCE 文件。
Xcode 给了我两个错误
/Tools/JUCE/modules/juce_core/containers/juce_Variant.h:41:18: Expected unqualified-id
和
/Tools/JUCE/modules/juce_core/containers/juce_Variant.h:343:46: Expected '>'
谢谢
由于只有静态方法,因此不需要该类型的对象。你应该能够这样称呼它:
typedef Point<int> MyPoint;
MyPoint point(1,2); // call constructor of a Point<int>
var x = VariantConverter::toVar(point); //call static methods of struct
MyPoint point2 = VariantConverter::fromVar(x);
template <>
struct VariantConverter<Point<int>>
{ /* ... */ };
您在这里拥有的是模板专业化。如果你想专攻,你首先需要一个基本模板,然后才能编译。
template <typename>
struct VariantConverter;
有时,如果只有专业化存在,它很有用,那么您只需将基础声明为上述,但是,票价通常具有默认实现:
template <typename T>
struct VariantConverter
{
static T fromVar(var const& v);
};
唯一地,在上面的例子中var
应该是什么?还有吗?VariantConverter
结构的另一个模板参数?第三个选项是我显示的选项:函数模板参数:
template <typename T>
struct VariantConverter
{
template <typename var>
static T fromVar(var const& v);
};
您现在可以提供适合大多数类型的默认实现 - 如果有合理的实现(否则,不实现默认值而只具有专用化可能更合适(。
好的,现在我们可以专攻:
template <>
struct VariantConverter<Point<int>>
// Point<int> now is what previously was T; could be any arbitrary type, not
// necessarily Point<int>, but std::string, double, ...
{
// still, you need to tell what var actually is
// maybe you have defined a class for already? similar to std::vector?
// assuming so this time...
static Point<int> fromVar (var const& v)
{
return Point<int>
(
// are you sure point accepts an initializer list?
// or do you intend to create first a point and then
// use move/copy constructor???
//{ v[0].operator int(), v[1].operator int() }
// rather normal constructor directly:
//v[0].operator int(), v[1].operator int()
// however, explicitly calling cast operators is ugly, rather just cast:
static_cast<int>(v[0]), static_cast<int>(<v[1])
// or C style casts, if you prefer
//(int)v[0], (int)v[1]
// I personally would rather go with the former, though, for being
// more "C++-ish"
);
}
};
现在,也许您正在寻找完全不同的东西:
template <typename T>
struct VariantConverter
{
static Point<T> fromVar(std::vector<T> const& v)
{
return Point<T>(v[0], v[1]);
}
};
最后,正如您明确要求的那样:
template <>
struct VariantConverter<Point<int>>
{
using Type = Point<int>;
static Type fromVar(/* ... */);
};
或者,对于第二个变体:
template <typename T>
struct VariantConverter
{
using Type = Point<T>;
static Type fromVar( /* ... */);
};
对不起,我的沟通不畅。 英语是我的第一语言,尽管受过教育,但我在书面交流方面没有天赋。 这是更大的背景。 我正在尝试添加到现有类中。
//This is the end of their code
//==============================================================================
/** This template-overloaded class can be used to convert between var and custom types.
@tags{Core}
*/
template <class Type> struct VariantConverter
{
static Type fromVar (const var& v) { return static_cast<Type> (v); }
static var toVar (const Type& t) { return t; }
};
//here is my code
template <> struct VariantConverter<Point<int>>
{
static Point<int> fromVar (const var& v)
{
return Point<int>
(
{ v[0].operator int(), v[1].operator int() }
);
}
static var toVar (const Point<int>& p)
{
return Array<var>({ p.getX(), p.getY() });
}
};
相关文章:
- 模板元编程:如何将参数包组合成新的参数包
- C++模板函数中的初始化 - 新的初始值设定项表达式列表被视为复合表达式
- VS2015/VC++ 在新类模板中禁用默认 #include "stdafx.h"
- 如何在新模板中使用模板化类?
- 如何在使用 ItemType 的模板类时将新节点插入二叉树 c++.(我是 c++ 的新手)
- 如何从 C++ 中的现有模板函数定义新函数
- 为模板参数类型中的新对象分配内存
- 使用新表达式的模板参数推断失败
- 专用模板类并添加新接口
- 取消脱壳:使用模板在多行类型定义中添加一个尖括号后的新行
- 没有新成员的模板多态性派生类的大小
- C 模板:返回新类型的正确方法
- 使用模板在类中声明新函数
- 我如何将现有模板类部分分为新类型
- C 17中的新:默认情况下匹配的模板模板
- 在C 中,是否有一种(新的)方法来测试类是否在模板中具有虚拟破坏者而无需实例化实例
- 使用新模板的类中的模板专用化
- 引入新模板参数的模板类的模板友元函数
- 是否可以强制新模板实例化
- 部分模板绑定,将新模板创建为类型