Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Samples for non...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 9523 of 9828
Post > Topic >>

Samples for non-type template argument other than integral types

by Marco Dorantes <marcodorantes@[EMAIL PROTECTED] > Apr 20, 2008 at 03:49 PM

In TC++PL Special Edition, template parameters section, page 332,
second paragraph said:
“A template argument can be a constant expression, the address of an
object or function with external linkage, or a non-overloaded pointer
to member.”

I have used type parameters and also constant expressions as non-type
template arguments in my programs. Now, I am experimenting with
further possibilities from the other types of template arguments. By
now, I have successfully experiment with the following:

* the address of an object
* the address of a function with external linkage,
* a non-overloaded pointer to member

I have compiled and executed these experiments:

template<void (*n)(const char*)>
class S
{
public:
	void f()
	{
		n("here");
	}
};

template<typename T, T (*n)(T)>
class V
{
public:
	void f(T t1)
	{
		T t2=n(t1);
		std::cout<<"\n"<<t2<<"\n";
	}
};

class Agent
{
public:
	void f(){std::cout<<"\nAgent::f()\n";}
	void e(){std::cout<<"\nAgent::e()\n";}
};
Agent agent;

template<void (Agent::*g)()>
class U
{
public:
	void f(Agent* a)
	{
		(a->*g)();
	}
};

template<typename T,void (T::*g)()>
class W
{
public:
	void f(T* a)
	{
		(a->*g)();
	}
};

template<Agent* a>
class Y
{
public:
	void f()
	{
		a->f();
	}
};

void g(const char* m){std::cout<<"g:"<<m;}
int g(int n){return n*2;}
double g(double n){return n*3;}
void g(Agent* a,void (Agent::*f)())
{
	(a->*f)();
}

void main()
{
	S<&g> s;
	s.f();
	V<int,&g> v1;
	v1.f(12);
	V<int,g> v11;
	v11.f(40);
	V<double,&g> v2;
	v2.f(3);

	g(&agent,&Agent::f);

	U<&Agent::f> u1;
	u1.f(&agent);

	U<&Agent::e> u2;
	u2.f(&agent);

	W<Agent,&Agent::f> w1;
	w1.f(&agent);

	W<Agent,&Agent::e> w2;
	w2.f(&agent);

	Y<&agent> y;
	y.f();
}
I am interested to discuss how others have used these other types of
non-type template arguments. Could you please point to typical uses
of?:

1- the address of an object
2- the address of a function with external linkage,
3- a non-overloaded pointer to member

Thank you very much in advance for any comment.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 2 Posts in Topic:
Samples for non-type template argument other than integral types
Marco Dorantes <marcod  2008-04-20 15:49:32 
Re: Samples for non-type template argument other than integral t
Lance Diduck <lancedid  2008-04-20 21:02:09 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:46:54 CDT 2008.