【发布时间】:2026-01-30 07:10:02
【问题描述】:
这是我测试 boost::tribool 的样本:
#include <iostream>
#include "boost/logic/tribool.hpp"
int main()
{
boost::logic::tribool init;
//init = boost::logic::indeterminate;
init = true;
//init = false;
if (boost::logic::indeterminate(init))
{
std::cout << "indeterminate -> " << std::boolalpha << init << std::endl;
}
else if (init)
{
std::cout << "true -> " << std::boolalpha << init << std::endl;
}
else if (!init)
{
std::cout << "false -> " << std::boolalpha << init << std::endl;
}
bool safe = init.safe_bool(); <<---- error here
if (safe) std::cout << std::boolalpha << safe << std::endl;
return 0;
}
我正在尝试使用 safe_bool() 函数将 boost::tribool 转换为纯 bool,但出现编译时错误:
Error 1 error C2274 : 'function-style cast' : illegal as right side of '.' operator D : \install\libs\boost\boost_samples\modules\tribool\src\main.cpp 23 1 tribool
看起来我错误地使用了 safe_bool() 函数。 你能帮我解决这个问题吗?谢谢。
【问题讨论】: