尝试使用标记调度失败:“无法从bool转换为std :: true_type”

时间:2015-02-07 08:33:32

标签: c++ templates

#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'

我不明白为什么会这样,我做错了什么。我过去使用过这个技巧没问题。

1 个答案:

答案 0 :(得分:2)

您无法根据要转换的内容的值进行隐式转换。鉴于b是模板bool参数,您可以执行

void f()
{
    fImpl(std::integral_constant<bool, b>());
}