由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - One c++ non-type template question
相关主题
where to define my template functionboost::function 的 syntax 问题
template问个C++ virtual function的问题 (转载)
私有成员不能用类成员函数修改?一个C++ template的问题
C++ Q04: template memberquestion for C++ constant
请教一下这个template function在gcc下要怎么修改C++ 的 问题
C++ class template specialization questiona simple question for C++ class
题2C++小程序查错
这个是个c++的bug 么?c++ question
相关话题的讨论汇总
话题: set话题: template话题: int话题: std话题: void
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
c++ question请教一下这个template function在gcc下要怎么修改
c++ template question:C++ class template specialization question
C++里get array size的问题 (转载)题2
which func will be called?这个是个c++的bug 么?
where to define my template functionboost::function 的 syntax 问题
template问个C++ virtual function的问题 (转载)
私有成员不能用类成员函数修改?一个C++ template的问题
C++ Q04: template memberquestion for C++ constant
相关话题的讨论汇总
话题: set话题: template话题: int话题: std话题: void