#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从'bool'转换为'std :: true_type'
我不明白为什么会这样,我做错了什么。我过去使用过这个技巧没问题。
答案 0 :(得分:2)
您无法根据要转换的内容的值进行隐式转换。鉴于b
是模板bool
参数,您可以执行
void f()
{
fImpl(std::integral_constant<bool, b>());
}