Giter VIP home page Giter VIP logo

tmplargs's Introduction

TmplArgs

我们可以通过函数重载为同名函数实现不同参数个数的版本,如:

int max(int a, int b) { return a>b?a:b; }
int max(int a, int b, int c)
{ return max(a,b)>max(b,c)?max(a,b):max(b,c); }

我们还可以通过使用函数模板来扩展函数对参数类型的定制,如:

template<typename _T>
void debug(_T a, std::string b) { cout<<a<<b<<endl; }
template<typename _T, typename _T1>
void debug(_T a, _T1 b, std::string c) { cout<<a<<b<<c<<endl; }

然而这种规则对于类模板却并不是(至少现在不是)奏效的。如:

template<typename _T> struct Debug {
	_T _a;
	Debug(_T a) : _a(a) {}
	void debug(std::string s) { cout<<_a<<s; }
};
template<typename _T, typename _T1> struct Debug {
	_T _a;
	_T1 _b;
	Debug(_T a, _T1 b) : _a(a), _b(b) {}
	void debug(std::string s) { cout<<_a<<_b<<s; }
};

上面的代码是不符合编译器(VS2010)规则的。也就是说我们没有办法为同名的类模板实现不同参数的版本。 那么我们有没有办法突破这个限制呢?是的,通过 TmplArgs 库,我们便获得了这种能力。

首先,我们需要实现类模板的宏定义式,如 DebugBase.h。
然后,我们需要实现类模板的通用形式,如 Debug.h。
最后,我们还需要将参数规则写成函数指针的形式。

下面这段代码就是使用 TmplArgs 后对上面的 Debug 模板的改写:

Debug<void(int)> a;
a.debug(3,"0");
Debug<void(int,string)> b;
b.debug(3, "4", "0");

tmplargs's People

Contributors

lvan100 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.