boost::variant - why is “const char*” converted to “bool”?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 21:29:53

问题


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;
}

output:

type: other -> 1
type: string -> some string

If I remove the bool type from MyVariant and change it to this:

typedef variant<string, int> MyVariant;

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?


回答1:


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");
}

output:

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");
}

output:

string

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.




回答2:


This has nothing to do with boost::variant, but with the order in which C++ selects the conversions to apply. Before trying to use user-defined conversions (remember that std::string is a user-defined class for this purpose), the compiler will try built-in conversions. There is no built-in conversion from const char* to int, but according to §4.12 in the standard:

A prvalue of [...] pointer [...] type can be converted to a prvalue of type bool.

So the compiler happily converts your const char* to a bool and never gets to consider converting it to a std::string.

UPDATE: It looks like this clearly unwanted conversion is being fixed. You can find a technical explanation of the fix here.



来源:https://stackoverflow.com/questions/13268608/boostvariant-why-is-const-char-converted-to-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!