更新时间:2023-11-30 22:20:28
如果您愿意接受一些新增的语法,则可以使用:
If you are open to a bit of added syntax, you can use:
// No default implementation.
template <typename T, T value> struct Impl;
// Implement the bool/true version
template <> struct Impl<bool, true>
{
void operator()() {}
};
// Implement the bool/false version
template <> struct Impl<bool, false>
{
void operator()() {}
};
// Implement the int version
template <int N> struct Impl<int, N>
{
void operator()() {}
};
template <typename T, T value>
void func()
{
Impl<T, value>()();
};
int main()
{
func<bool, true>();
func<int, 10>();
}
免责声明
我不知道这样做是否会比调用func(true)
更好.
I have no idea whether this will perform better than calling func(true)
.