s*****e 发帖数: 33 | 1 Not sure how this can be done, suppose I have a template function
template
void set()
{
std::cout << N << std::endl;
}
int main()
{
int t = 10;
// is it possible to call set() to print t?
// since t is not known during compile time, this won't work,
// I am just wondering whether there is a trick to make this work some
how, e.g., boost Int2Type?
} | t****t 发帖数: 6806 | 2 if it's unknown at compile time, no, it can not be done. however, you can
use address of int instead (since global variable address is known at
compile time):
template
void set()
{
cout<
}
int t;
int main()
{
/* get value for t */
set();
}
something like that, you may need to do some adjustment.
【在 s*****e 的大作中提到】 : Not sure how this can be done, suppose I have a template function : template : void set() : { : std::cout << N << std::endl; : } : int main() : { : int t = 10; : // is it possible to call set() to print t?
| V9 发帖数: 143 | 3 manybe constexpr can help
【在 t****t 的大作中提到】 : if it's unknown at compile time, no, it can not be done. however, you can : use address of int instead (since global variable address is known at : compile time): : template : void set() : { : cout<: } : int t; : int main()
| l********a 发帖数: 1154 | 4 不太了解你的需求
如果只是针对int类型有特殊操作,其他类型都调用模板函数,你可以明确对于int的set
函数(即使有同名template set() 存在),我目前项目有个类似的情况就
是这样解决的.
网上说这跟c++的函数名称搜索次序有关,我忘记在哪里看到的了
测试代码:
void set(int t)
{
std::cout << "print from normal function 1\n" << t << std::endl;
}
void set(string t)
{
std::cout << "print from normal function 2\n" << t.c_str() << std::endl;
}
template
void set(T t)
{
std::cout << "print from template function\n" << t << std::endl;
}
int main()
{
int it = 10;
float ft = 10.0;
double dt = 20.53;
string st = "test string";
set(it);
set(ft);
set(dt);
set(st);
return 0;
} | s*****e 发帖数: 33 | 5 Sorry, this is the original intention, what I want to achieve is some kind
of automatic dispatch, say we have 3 specialized functions:
like:
template
void set()
{
std::cout << "set " << N << " is called\n";
}
template<>
void set<1>()
{
}
template<>
void set<2>()
{
}
template<>
void set<3>()
{
std::cout << "3 called" << std::endl;
}
int main()
{
int n;
// read n from somewhere, e.g.,network or disk
switch (n)
{
case 0: set<0>(); break;
case 1: set<1)(); break;
....
}
//intention is to replace this switch with some trick,
//since n is not known at compile time, seems impossible
}
set
【在 l********a 的大作中提到】 : 不太了解你的需求 : 如果只是针对int类型有特殊操作,其他类型都调用模板函数,你可以明确对于int的set : 函数(即使有同名template set() 存在),我目前项目有个类似的情况就 : 是这样解决的. : 网上说这跟c++的函数名称搜索次序有关,我忘记在哪里看到的了 : 测试代码: : void set(int t) : { : std::cout << "print from normal function 1\n" << t << std::endl; : }
| l********a 发帖数: 1154 | 6 why not
template
void set(T t, int mode)
{
std::cout << "print from set<" << mode << "> of template function\n" <<
t << std::endl;
}
int main()
{
int data = 20;
for (int i=0;i<3;i++)
{
set(data,i);
}
}
【在 s*****e 的大作中提到】 : Sorry, this is the original intention, what I want to achieve is some kind : of automatic dispatch, say we have 3 specialized functions: : like: : template : void set() : { : std::cout << "set " << N << " is called\n"; : } : template<> : void set<1>()
| t****t 发帖数: 6806 | 7 你这写的什么玩艺儿?
<
【在 l********a 的大作中提到】 : why not : template : void set(T t, int mode) : { : std::cout << "print from set<" << mode << "> of template function\n" << : t << std::endl; : } : int main() : { : int data = 20;
| l*********s 发帖数: 5409 | 8 have a array of function pointers, done. |
|