我想要一个看起来像我下面的模板类.然后,我想要一个带有模板特化的函数,具体取决于CLASS模板参数.我该如何工作?我意识到我提供的代码在很多层面都是错误的,但它只是为了说明这个概念.
template <typename _T, size_t num>
class Foo
{
// If num == 1, I want to call this function...
void Func<_T, 1>()
{
printf("Hi!");
}
// Otherwise, I want to call this version.
void Func<_T, num>()
{
printf("Hello world!");
}
};
Run Code Online (Sandbox Code Playgroud)
struct Otherwise { };
template<size_t> struct C : Otherwise { };
// don't use _Uppercase - those names are reserved for the implementation
// (i removed the '_' char)
template <typename T, size_t num>
class Foo
{
public:
void Func() { Func(C<num>()); }
private:
// If num == 1, I want to call this function...
void Func(C<1>)
{
printf("Hi 1!");
}
// If num == 2, I want to call this function...
void Func(C<2>)
{
printf("Hi 2!");
}
// Otherwise, I want to call this version.
void Func(Otherwise)
{
printf("Hello world!");
}
//// alternative otherwise solution:
// template<size_t I>
// void Func(C<I>) { .. }
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1442 次 |
| 最近记录: |