如何在新模板中使用模板化类?

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() });
}
};