将特化定义为另一个 Class 的实例化
Define a Specialization As Instantiation of Another Class
我有一种情况,我想定义一个特化 与 另一个 class 的实例化相同。这是我想要的一个简单示例(此示例中的完整实例化;在我的实际问题中,我想要部分专业化):
//Template class
template <int i> class Foo { /*...*/ };
//Another, completely different, template class
template <int i,int j> class Bar { /*...*/ };
//Ordinary specialization of Foo
template <> class Foo<0> { /*...*/ };
//Bogus specializations of Foo that
// hopefully explain what I want.
template <> class Foo<1> = Bar<1, 11>;
template <> class Foo<2> = Bar<2, 42>;
template <> class Foo<3> = Bar<3,-71>;
...
这样做有什么好处? Foo<1>
、Foo<2>
等的定义比较复杂,但是一次写成模板很容易。有很多这样的定义。将 Bar
的定义应用到 Foo
中不是一个选项。只有一些值可以特化,Bar
的特化必须手动选择(因此 int j
的随机数)。
我通常会使用CRTP 实现这种效果。我会对 Bar
做一些相当讨厌的修改,然后做类似的事情:
template <> class Foo<1> : public Bar<1, 11,Foo<1>> {};
template <> class Foo<2> : public Bar<2, 42,Foo<2>> {};
template <> class Foo<3> : public Bar<3,-71,Foo<3>> {};
...
显然,Bar
需要更改,可能需要一些 using
声明来拉下构造函数。这样会很乱。
我的问题:我可以做得比这更好吗?也就是说,是否可以将特化定义为另一个模板的实例化class?
注意:首选标准是 C++14,尽管更高版本也可以接受。
您可以使用继承代替 CRTP。
template <int i> struct Foo {};
template <int i,int j> struct Bar { Bar() {} };
template <> struct Foo<0> {};
template <> struct Foo<1> : public Bar<1, 11> { using Bar::Bar; };
template <> struct Foo<2> : public Bar<2, 42> { using Bar::Bar; };
template <> struct Foo<3> : public Bar<3,-71> { using Bar::Bar; };
间接寻址有帮助吗? :)
template<int i> struct Indirection { using type = Foo<i>; };
template <> class Indirection<1> { using type = Bar<1, 11>; };
template <> class Indirection<2> { using type = Bar<2, 42>; };
template <> class Indirection<3> { using type = Bar<3, -71>; };
template<int i>
using ActualFoo = typename Indirection<i>::type;
我有一种情况,我想定义一个特化 与 另一个 class 的实例化相同。这是我想要的一个简单示例(此示例中的完整实例化;在我的实际问题中,我想要部分专业化):
//Template class
template <int i> class Foo { /*...*/ };
//Another, completely different, template class
template <int i,int j> class Bar { /*...*/ };
//Ordinary specialization of Foo
template <> class Foo<0> { /*...*/ };
//Bogus specializations of Foo that
// hopefully explain what I want.
template <> class Foo<1> = Bar<1, 11>;
template <> class Foo<2> = Bar<2, 42>;
template <> class Foo<3> = Bar<3,-71>;
...
这样做有什么好处? Foo<1>
、Foo<2>
等的定义比较复杂,但是一次写成模板很容易。有很多这样的定义。将 Bar
的定义应用到 Foo
中不是一个选项。只有一些值可以特化,Bar
的特化必须手动选择(因此 int j
的随机数)。
我通常会使用CRTP 实现这种效果。我会对 Bar
做一些相当讨厌的修改,然后做类似的事情:
template <> class Foo<1> : public Bar<1, 11,Foo<1>> {};
template <> class Foo<2> : public Bar<2, 42,Foo<2>> {};
template <> class Foo<3> : public Bar<3,-71,Foo<3>> {};
...
显然,Bar
需要更改,可能需要一些 using
声明来拉下构造函数。这样会很乱。
我的问题:我可以做得比这更好吗?也就是说,是否可以将特化定义为另一个模板的实例化class?
注意:首选标准是 C++14,尽管更高版本也可以接受。
您可以使用继承代替 CRTP。
template <int i> struct Foo {};
template <int i,int j> struct Bar { Bar() {} };
template <> struct Foo<0> {};
template <> struct Foo<1> : public Bar<1, 11> { using Bar::Bar; };
template <> struct Foo<2> : public Bar<2, 42> { using Bar::Bar; };
template <> struct Foo<3> : public Bar<3,-71> { using Bar::Bar; };
间接寻址有帮助吗? :)
template<int i> struct Indirection { using type = Foo<i>; };
template <> class Indirection<1> { using type = Bar<1, 11>; };
template <> class Indirection<2> { using type = Bar<2, 42>; };
template <> class Indirection<3> { using type = Bar<3, -71>; };
template<int i>
using ActualFoo = typename Indirection<i>::type;