C++ 尝试使用标记分派失败:“0”;无法从bool转换为std::true“U类型”; #包括 #包括 模板 结构条件 { void f() { fImpl(b); } 私人: void fImpl(标准::真实类型) { std::cout

C++ 尝试使用标记分派失败:“0”;无法从bool转换为std::true“U类型”; #包括 #包括 模板 结构条件 { void f() { fImpl(b); } 私人: void fImpl(标准::真实类型) { std::cout,c++,templates,C++,Templates,您不能基于正在转换的内容的值进行隐式转换。如果b是一个模板bool参数,您可以这样做 #include <iostream> #include <type_traits> template <bool b> struct Conditional { void f() { fImpl(b); } private: void fImpl(std::true_type) { std::cou

您不能基于正在转换的内容的值进行隐式转换。如果
b
是一个模板
bool
参数,您可以这样做

#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();
}
void f()
{
fImpl(std::integral_constant());
}

我不知怎的认为,
std::integral\u constant
有一个来自value的构造函数。显式转换并不简洁。。。
void f()
{
    fImpl(std::integral_constant<bool, b>());
}