c – 具有不同签名的类方法专业化
发布时间:2020-12-16 07:34:08 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个类模板,其中方法签名会根据模板参数而改变.我的目标是尽可能少地复制代码.考虑这个例子,首先是类声明: // a.hxx#ifndef A_HXX#define A_HXXtemplatetypename Tstruct A{ void foo(T value) const; void bar() const;};#include string#if
我正在尝试编写一个类模板,其中方法签名会根据模板参数而改变.我的目标是尽可能少地复制代码.考虑这个例子,首先是类声明:
// a.hxx #ifndef A_HXX #define A_HXX template<typename T> struct A { void foo(T value) const; void bar() const; }; #include <string> #ifndef short_declaration template<> struct A<std::string> { void foo(const std::string &value) const; void bar() const; }; #else // short_declaration template<> struct A<std::string> { void foo(const std::string &value) const; }; #endif // short_declaration #endif // A_HXX 现在类定义: // a_impl.hxx #ifndef A_IMPL_HXX #define A_IMPL_HXX #include "a.hxx" #include <iostream> #include <typeinfo> template<typename T> void A<T>::foo(T value) const { std::cout << "A<T=" << typeid(T).name() << ">::foo(" << value << ")" << std::endl; } template<typename T> void A<T>::bar() const { std::cout << "A<T=" << typeid(T).name() << ">::bar()" << std::endl; } void A<std::string>::foo(const std::string &value) const { std::cout << "A<std::string>::foo(" << value << ")" << std::endl; } #ifndef skip_duplicates void A<std::string>::bar() const { std::cout << "A<std::string>::bar()" << std::endl; } #endif // skip_duplicates #endif // A_IMPL_HXX 现在是一个测试程序: // test.cxx //#define skip_duplicates //#define short_declaration #include "a_impl.hxx" int main(void) { A<int> obj1; A<std::string> obj2; int value1(1); std::string value2("baz"); obj1.foo(value1); obj1.bar(); obj2.foo(value2); obj2.bar(); return 0; } 如果像这样编译,我得到的预期输出(对于我的typeid实现): A<T=i>::foo(1) A<T=i>::bar() A<std::string>::foo(baz) A<std::string>::bar() 但我当然喜欢在我的例子中启用skip_duplicates甚至short_declaration的方法.有点similar question,ecatmur回复说需要给出完整的类,所以至少定义short_declaration是行不通的. 其他人如何使用可能将大对象作为参数的方法来处理创建类模板的问题? 解决方法
您可以将重复项提取到基类中:
template<typename T> struct Base{ void Bar() { std::cout << "A<T=" << typeid(T).name() << ">::bar()" << std::endl; } protected: ~Base(){} template<typename U> void DoFoo(U value) { std::cout << "A<T=" << typeid(T).name() << ">::foo(" << value << ")" << std::endl; } }; template<typename T> struct A : Base<T> { void Foo(T value) { DoFoo(value); } }; template<> struct A<std::string> : Base<std::string> { void Foo(const std::string& value) { DoFoo(value); } }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |