gpt4 book ai didi

c++ - 尝试使用标签调度 : "cannot convert from bool to std::true_type" 失败

转载 作者:搜寻专家 更新时间:2023-10-31 02:24:11 26 4
gpt4 key购买 nike

#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();
}

上面的代码产生错误:

cannot convert argument 1 from 'bool' to 'std::true_type'

我不明白为什么会这样以及我做错了什么。我过去曾毫无问题地使用过这个技巧。

最佳答案

您不能根据正在转换的任何值进行隐式转换。鉴于 b 是一个模板 bool 参数,你可以这样做

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

关于c++ - 尝试使用标签调度 : "cannot convert from bool to std::true_type" 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28380123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com