字符串布尔比较-为什么? [英] String-bool comparsion - why?

查看:146
本文介绍了字符串布尔比较-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到意外行为时,我正在使用 boost :: variant< int,std :: string,bool> 及其访问者:字符串和布尔值是可比的。我不知道为什么它会这样工作,但是我发现它很有趣。我唯一的想法是将具有bool值的变体解释为char?有人可以向我解释吗?
比较访问者:

I was working with boost::variant<int,std::string,bool> and its visitors when I runned into an unexpected behavior: the string and bool values were comparable. I don't know, why does it work like this, but I found it interesting. My only idea is that the variant with the bool value was interpreted as a char? Someone could explain it to me? The comparsion visitor:

#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/variant.hpp>
#include <boost/function.hpp>

struct my_less : boost::static_visitor<bool*>
{
   template<typename T>
   bool* operator()(T a, T b) const
   {
       return a<b ? new bool(true) : new bool(false);
   }

   template<typename T, typename U>
   bool* operator()(T a, U b) const
   {
       return NULL;
   }
};

int main()
{
typedef boost::variant<int,bool,std::string> datatype;
datatype *a = new datatype(false);
datatype *b = new datatype("abc");

my_less cmp;

bool* val = boost::apply_visitor(cmp,*a,*b);

if(val)
{
    std::cout << *val;
}
else
{
    std::cout << "NULL";
}

}

编辑
这是具有一些测试用例的扩展主函数:

EDIT Here is an extended main function with some test cases:

void show_result(bool* val)
{
if(val)
{
    std::cout << *val << std::endl;
}
else
{
    std::cout << "NULL" << std::endl;
}
}

int main()
{
//std::string a = "bbb";
//bool b = true;
//std::cout << b<a;      //compilation error

typedef boost::variant<int,bool,std::string> datatype;
datatype int_value_1(4);
datatype int_value_2(3);
datatype string_value("abc");
datatype bool_value(true);
my_less cmp;

std::cout<<"First result, compare ints 4 and 3:"<<std::endl;
bool* val = boost::apply_visitor(cmp,int_value_1,int_value_2);
show_result(val);

std::cout<<"Second result, compare int to string 4 to abc " << std::endl;
val = boost::apply_visitor(cmp,int_value_1,string_value);
show_result(val);

std::cout <<"Third result, int 4 to bool true:" << std::endl;
val = boost::apply_visitor(cmp,int_value_1,bool_value);
show_result(val);

std::cout<<"Fourth result, string abc to bool true" << std::endl;
val = boost::apply_visitor(cmp,string_value,bool_value);
show_result(val);

}

输出:

First result, compare ints 4 and 3:
0
Second result, compare int to string 4 to abc
NULL
Third result, int 4 to bool true:
NULL
Fourth result, string abc to bool true
0


推荐答案

好的,既然您已经完全更改了程序,让我再试一次。

OK, now that you've completely changed your program, let me try again.

问题是:

datatype *b = new datatype("abc");

abc const char * ,而不是 std :: string 。如果要创建 std :: string 变体,则需要显式地创建。否则,您将最终创建一个 bool 变体,因为所有指针都可以转换为 bool ,包括 const char * 指针。

"abc" is a const char*, not a std::string. If you want to create a std::string variant, you need to do so explicitly. Otherwise, you'll end up creating a bool variant because all pointers are convertible to bool, including const char* pointers.

尝试一下

datatype *b = new datatype(std::string("abc"));






bool之间的这种相互作用 std :: string 显然是众所周知的,并且有些恼人。 boost :: variant 提供了模板化的构造函数,但是解析规则更喜欢使用内置对话而不是 bool ,并且没有C ++中的一种方法,用于在构造函数上指定模板特化。可以显式地专门化分配,因此您可以这样写:


This interaction between bool and std::string is apparently well-known and somewhat irritating. boost::variant provides a templated constructor, but the resolution rules prefer the built-in converstion to bool and there's no way in C++ to specify a template specialization on a constructor. It is possible to explicitly specialize assignment, so you can write:

datatype b;
b.operator=<std::string>("abc");

效率可能略高,但可读性比

which might be marginally more efficient but much less readable than

datatype b;
b = std::string("abc");

如果您不包括 bool 作为一个变体,然后字符串文字会自动转换为 std :: string 。也许可以使用某种代理伪布尔类。我从未尝试过。

If you don't include bool as a variant, then string literals do automatically convert to std::string. Maybe it's possible to use some sort of proxy pseudo-boolean class. I've never tried.

这篇关于字符串布尔比较-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆