非类型模板参数 [英] Nontype template parameter
问题描述
我在使用nontype(int variable)模板参数时遇到问题。
为什么我不能将常量int变量传递给函数,并让函数实例化模板?
模板< int size>
class MyTemplate
{
// do something with size
};
void run(const int j)
{
MyTemplate< j> b; // not fine
}
void main()
{
const int i = 3;
MyTemplate< i>一个; // fine;
run(i); // not fine
}
不好:编译器说,错误:'j'出现在常数表达式中
- 编辑
这是我结束了。
也许有人可能会使用它,有人可能会建议更好的方式。
枚举PRE_SIZE
{
PRE_SIZE_256 = 256,
PRE_SIZE_512 = 512,
PRE_SIZE_1024 = 1024,
};
template< int size>
class SizedPool:public Singleton< SizedPool< size> >
{
public:
SizedPool()
:mPool(size)
{
}
void * Malloc {
return mPool.malloc();
}
void Free(void * memoryPtr)
{
mPool.free(memoryPtr);
}
private:
boost :: pool<> mPool;
};
template< int size>
void * SizedPoolMalloc()
{
return SizedPool< size> :: GetInstance() - > Malloc
}
template< int size>
void SizedPoolFree(void * memoryPtr)
{
SizedPool< size> :: GetInstance() - > Free(memoryPtr);
}
void * SizedPoolMalloc(int size)
{
if(size< = PRE_SIZE_256)
return SizedPoolMalloc< PRE_SIZE_256>();
else if(size< = PRE_SIZE_512)
return SizedPoolMalloc< PRE_SIZE_512>();
}
void toRun(const int j)
{
SizedPoolMalloc(j);
}
void Test17()
{
const int i = 3;
toRun(i);
}
在编译时需要值。记住,模板是编译时机制;模板不存在于最终可执行文件中。还要记住,函数和函数的参数传递都是运行时机制。在 run()
中的 j
参数的值将不会被知道,直到程序实际运行并调用 run()
函数,在编译阶段之后。
void run(const int j)
{
//编译器无法知道是直到程序实际运行!
MyTemplate< j> b;
}
const int i = 3;
run(i);
这就是为什么编译器抱怨说'j'不能出现在常量表达式。
另一方面,这是很好的,因为 i
的值在编译时是已知的。
const int i = 3;
//编译器知道我现在有值3,
//所以我们可以实际编译这个。
MyTemplate< i>一个;
您可以将编译时值传递给运行时构造, / p>
但是,您可以使用 run()
函数接受非类型模板参数,方法与 MyTemplate
模板类接受非类型模板参数:
template< int j> ;
void run()
{
MyTemplate< j> b;
}
const int i = 3;
run< i>();
I'm having trouble with nontype(int variable) template parameter.
Why can't I pass a constant int variable to a function and let the function instantiate the template?
template<int size>
class MyTemplate
{
// do something with size
};
void run(const int j)
{
MyTemplate<j> b; // not fine
}
void main()
{
const int i = 3;
MyTemplate<i> a; // fine;
run(i); // not fine
}
not fine : compiler says, error: 'j' cannot appear in constant-expression
- EDIT
This is what I ended up with. Maybe someone might use it, someone might suggest better way.
enum PRE_SIZE
{
PRE_SIZE_256 = 256,
PRE_SIZE_512 = 512,
PRE_SIZE_1024 = 1024,
};
template<int size>
class SizedPool : public Singleton< SizedPool<size> >
{
public:
SizedPool()
: mPool(size)
{
}
void* Malloc()
{
return mPool.malloc();
}
void Free(void* memoryPtr)
{
mPool.free(memoryPtr);
}
private:
boost::pool<> mPool;
};
template<int size>
void* SizedPoolMalloc()
{
return SizedPool<size>::GetInstance()->Malloc();
}
template<int size>
void SizedPoolFree(void* memoryPtr)
{
SizedPool<size>::GetInstance()->Free(memoryPtr);
}
void* SizedPoolMalloc(int size)
{
if (size <= PRE_SIZE_256)
return SizedPoolMalloc<PRE_SIZE_256>();
else if (size <= PRE_SIZE_512)
return SizedPoolMalloc<PRE_SIZE_512>();
}
void toRun(const int j)
{
SizedPoolMalloc(j);
}
void Test17()
{
const int i = 3;
toRun(i);
}
Because non-type template parameters require values at compile-time. Remember that templates are a compile-time mechanism; templates do not exist in the final executable. Also remember that functions and the passing of arguments to functions are runtime mechanisms. The value of the j
parameter in run()
will not be known until the program actually runs and invokes the run()
function, well past after the compilation stage.
void run(const int j)
{
// The compiler can't know what j is until the program actually runs!
MyTemplate<j> b;
}
const int i = 3;
run(i);
That's why the compiler complains says "'j' cannot appear in constant-expression".
On the other hand, this is fine because the value of i
is known at compile-time.
const int i = 3;
// The compiler knows i has the value 3 at this point,
// so we can actually compile this.
MyTemplate<i> a;
You can pass compile-time values to run-time constructs, but not the other way around.
However, you can have your run()
function accept a non-type template parameter the same way your MyTemplate
template class accepts a non-type template parameter:
template<int j>
void run()
{
MyTemplate<j> b;
}
const int i = 3;
run<i>();
这篇关于非类型模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!