如何为模板化的模板函数定义模板特化 class
How to define a template specialization for a templated function for a templated class
我有一个 class,类似于下面的 BizClass
。我想为特定的具体类型定义一个模板化函数和一个专门化。我不确定合适的语法。
template <class Foo>
class BizClass {
public:
template <typename Bar>
void Action(Container<Bar> m);
// Foo is used somewhere else...
};
// This seems to work.
template <class Foo>
template <typename Bar>
inline void BizClass<Foo>::Action(Container<Bar> m) {}
// This specialization doesn't compile, where Foobar is a concrete class.
template <class Foo>
inline void BizClass<Foo>::Action(Container<FooBar> m) {}
如何处理这种情况下的模板专业化?
How do I handle the template specialization for this case?
据我所知,C++ 禁止在模板 struct/class 内特化方法,而不特化 struct/class 本身。
但是你可以使用重载:使Action (Container<FooBar> m)
成为一个非模板方法
#include <iostream>
template <typename>
struct Container
{ };
struct FooBar
{ };
template <typename Foo>
struct BizClass
{
template <typename Bar>
void Action (Container<Bar> m);
void Action (Container<FooBar> m);
};
template <typename Foo>
template <typename Bar>
inline void BizClass<Foo>::Action (Container<Bar> m)
{ std::cout << "Action() generic version" << std::endl; }
template <typename Foo>
inline void BizClass<Foo>::Action (Container<FooBar> m)
{ std::cout << "Action() FooBar version" << std::endl; }
int main ()
{
BizClass<int> bci;
bci.Action(Container<int>{}); // print Action() generic version
bci.Action(Container<FooBar>{}); // print Action() FooBar version
}
在标题为 This seems to work.
的第一个代码示例中,它只是为模板函数定义 body,它不是特化。
在第二个示例中,您尝试特化一个模板,该模板是另一个未特化模板的成员。这是不允许的。
如果你想专攻 Action
,你还需要专攻 BizClass
,例如这是 Foo=int
和 Bar=char
的示例:
template<> template<>
inline void BizClass<int>::Action(Container<char> m)
{
}
我有一个 class,类似于下面的 BizClass
。我想为特定的具体类型定义一个模板化函数和一个专门化。我不确定合适的语法。
template <class Foo>
class BizClass {
public:
template <typename Bar>
void Action(Container<Bar> m);
// Foo is used somewhere else...
};
// This seems to work.
template <class Foo>
template <typename Bar>
inline void BizClass<Foo>::Action(Container<Bar> m) {}
// This specialization doesn't compile, where Foobar is a concrete class.
template <class Foo>
inline void BizClass<Foo>::Action(Container<FooBar> m) {}
如何处理这种情况下的模板专业化?
How do I handle the template specialization for this case?
据我所知,C++ 禁止在模板 struct/class 内特化方法,而不特化 struct/class 本身。
但是你可以使用重载:使Action (Container<FooBar> m)
成为一个非模板方法
#include <iostream>
template <typename>
struct Container
{ };
struct FooBar
{ };
template <typename Foo>
struct BizClass
{
template <typename Bar>
void Action (Container<Bar> m);
void Action (Container<FooBar> m);
};
template <typename Foo>
template <typename Bar>
inline void BizClass<Foo>::Action (Container<Bar> m)
{ std::cout << "Action() generic version" << std::endl; }
template <typename Foo>
inline void BizClass<Foo>::Action (Container<FooBar> m)
{ std::cout << "Action() FooBar version" << std::endl; }
int main ()
{
BizClass<int> bci;
bci.Action(Container<int>{}); // print Action() generic version
bci.Action(Container<FooBar>{}); // print Action() FooBar version
}
在标题为 This seems to work.
的第一个代码示例中,它只是为模板函数定义 body,它不是特化。
在第二个示例中,您尝试特化一个模板,该模板是另一个未特化模板的成员。这是不允许的。
如果你想专攻 Action
,你还需要专攻 BizClass
,例如这是 Foo=int
和 Bar=char
的示例:
template<> template<>
inline void BizClass<int>::Action(Container<char> m)
{
}