提高::变种 - 这是为什么"为const char *"转换到"布尔"? [英] boost::variant - why is "const char*" converted to "bool"?
问题描述
我已经声明了一个的boost ::变种
它接受三种类型:字符串
,布尔
和 INT
。下面code是显示我的变种接受为const char *
,并将其转换为布尔
。它是用于的boost ::变种
来接受并进行类型转换不是它的列表中的正常行为?
I have declared a boost::variant
which accepts three types: string
, bool
and int
. The following code is showing that my variant accepts const char*
and converts it to bool
. Is it a normal behavior for boost::variant
to accept and convert types not on its list?
#include <iostream>
#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"
using namespace std;
using namespace boost;
typedef variant<string, bool, int> MyVariant;
class TestVariant
: public boost::static_visitor<>
{
public:
void operator()(string &v) const
{
cout << "type: string -> " << v << endl;
}
template<typename U>
void operator()(U &v)const
{
cout << "type: other -> " << v << endl;
}
};
int main(int argc, char **argv)
{
MyVariant s1 = "some string";
apply_visitor(TestVariant(), s1);
MyVariant s2 = string("some string");
apply_visitor(TestVariant(), s2);
return 0;
}
输出:
类型:其他 - > 1结果
类型:字符串 - >一些字符串
type: other -> 1
type: string -> some string
如果我从MyVariant删除布尔类型,并将其改成这样:
If I remove the bool type from MyVariant and change it to this:
typedef variant<string, int> MyVariant;
为const char *
没有到布尔
更多转换。这一次,它会转换为字符串
,这是新的输出:
const char*
is no more converted to bool
. This time it's converted to string
and this is the new output:
类型:字符串 - >一些字符串结果
类型:字符串 - >一些字符串
type: string -> some string
type: string -> some string
这表明变量
首先尝试将其他类型布尔
再到字符串
。如果类型转换是必然的事情,应该总是发生,有没有什么办法让转化为字符串
更高的优先级?
This indicates that variant
tries to convert other types first to bool
and then to string
. If the type conversion is something inevitable and should always happen, is there any way to give conversion to string
a higher priority?
推荐答案
我不认为这是什么,尤其是与的boost ::变种
,它是关于哪个构造函数将通过重载决议选择。同样的事情发生与重载函数:
I don't think this is anything particularly to do with boost::variant
, it's about which constructor gets selected by overload resolution. The same thing happens with an overloaded function:
#include <iostream>
#include <string>
void foo(bool) {
std::cout << "bool\n";
}
void foo(std::string) {
std::cout << "string\n";
}
int main() {
foo("hi");
}
输出:
bool
我不知道的方式来改变什么构造一个变种
I don't know of a way to change what constructors a Variant has [edit: as James says, you can write another class that uses the Variant in its implementation. Then you can provide a const char*
constructor that does the right thing.]
也许你可以改变的变异类型。另一个重载例如:
Maybe you could change the types in the Variant. Another overloading example:
struct MyBool {
bool val;
explicit MyBool(bool val) : val(val) {}
};
void bar(MyBool) {
std::cout << "bool\n";
}
void bar(const std::string &) {
std::cout << "string\n";
}
int main() {
bar("hi");
}
输出:
string
不幸的是,现在你必须写巴(MyBool(真))
而不是的富(真)
。甚至在你变的情况下雪上加霜与串/布尔/ INT
,如果你只是将其更改为的一个变种串/ MyBool / INT
然后 MyVariant(真)
将调用 INT
构造。
Unfortunately now you have to write bar(MyBool(true))
instead of foo(true)
. Even worse in the case of your variant with string/bool/int
, if you just change it to a variant of string/MyBool/int
then MyVariant(true)
would call the int
constructor.
这篇关于提高::变种 - 这是为什么&QUOT;为const char *&QUOT;转换到&QUOT;布尔&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!