c – boost :: function如何支持具有不同长度模板参数的模板类
发布时间:2020-12-16 06:57:48 所属栏目:百科 来源:网络整理
导读:我想使用boost预处理器来声明具有不同模板变量长度的模板类,基本上就像boost :: function那样. #if !BOOST_PP_IS_ITERATING#ifndef D_EXAMPLE_H#define D_EXAMPLE_H#include boost/function#include boost/preprocessor/iteration/iterate.hpp#define BOOST_
我想使用boost预处理器来声明具有不同模板变量长度的模板类,基本上就像boost :: function那样.
#if !BOOST_PP_IS_ITERATING #ifndef D_EXAMPLE_H #define D_EXAMPLE_H #include <boost/function> #include <boost/preprocessor/iteration/iterate.hpp> #define BOOST_PP_ITERATION_PARAMS_1 (3,(1,2,"example.h")) #include BOOST_PP_ITERATE() #else template<class T,BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),class T)> class Example { boost::function<T,(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(),T))> func; }; #endif 上面的代码显然不会起作用,因为它在同一个头文件中声明了具有不同模板变量长度的同一个类.我想要实现的是包含一个文件并定义具有不同模板变量长度的类,就像boost :: function一样. #include "example.h" Example<int,int,float> example1; Example<double,int> example2; 我查了一下boost :: function的代码,但我无法弄清楚它是如何工作的.有任何想法吗? 解决方法
您需要首先使用最多参数声明模板类,并使用除第一个参数之外的所有参数的默认值.然后可以将具有较少参数的模板类定义为主模板类的特化.例:
#include <iostream> template<class A,class B = void,class C = void> class Example { public: static const int x = 3; }; template<class A,class B> class Example<A,B,void> { public: static const int x = 2; }; template<class A> class Example<A,void,void> { public: static const int x = 1; }; int main() { Example<int,int> e3; Example<int,int> e2; Example<int> e1; std::cout << e3.x << e2.x << e1.x << std::endl; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 正则表达式实战
- alignas说明符vs __attribute __(aligned),c 11
- postgresql安装完成后的操作
- Cocos2d-x 3.0final 终结者系列教程21ScrollView原理与使用
- objective-c – 使用Doxygen使用@see命令引用特定的类方法
- 关于Oracle to_date函数的高级用法
- cocos2dx luabinging
- ruby-on-rails – 将Jasper集成到Rails 3中
- ruby-on-rails – 如何在多站点应用程序上设置子域以进行集
- ruby-on-rails – 由于id = null,Rails has_many无法以嵌套