#include <iostream>
#include <type_traits>
template <bool b>
struct Conditional
{
void f()
{
fImpl(b);
}
private:
void fImpl(std::true_type)
{
std::cout << "true";
}
void fImpl(std::false_type)
{
std::cout << "false";
}
};
void main()
{
Conditional<true>().f();
}
上面的代码产生错误:
无法将参数 1 从"布尔"转换为"std::true_type">
我不明白为什么会发生这种情况以及我做错了什么。我过去用过这个技巧没有问题。
不能根据
要转换的任何内容的值进行隐式转换。鉴于b
是模板bool
参数,您可以
void f()
{
fImpl(std::integral_constant<bool, b>());
}