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! ]


|