Baz
Baz

Reputation: 13145

Compiler warning when using boost::blank in a boost::variant

I'm getting a warning for the following code, which dissapears if I remove boost::blank from the variant:

namespace DB
{
struct Value{};
struct Container{};
}

typedef boost::variant <boost::blank, DB::Value, DB::Container> CommandData;

struct Command {
    explicit Command(CommandData& _data): data(_data){
    }

    CommandData data;
};

int main()
{
    CommandData commandData;
    Command command(commandData);
    return 0;
}

What's this issue?

Here's the warning:

1>: warning C4345: behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
1>          c:\boost_1_49_0\boost\variant\variant.hpp(1224) : while compiling class template member function 'boost::variant<T0_,T1,T2>::variant(void)'
1>          with
1>          [
1>              T0_=boost::blank,
1>              T1=DB::Value,
1>              T2=DB::Container
1>          ]
1>          c:\code.h(38) : see reference to class template instantiation 'boost::variant<T0_,T1,T2>' being compiled
1>          with
1>          [
1>              T0_=boost::blank,
1>              T1=DB::Value,
1>              T2=DB::Container
1>          ]

Upvotes: 2

Views: 1037

Answers (2)

pmr
pmr

Reputation: 59841

That warning is rather dumb. It warns that MSVC now does the right thing as opposed to some ancient version. You can turn it off with a pragma.

Upvotes: 7

Alexander Reshytko
Alexander Reshytko

Reputation: 2246

It's not because of the variant. Try to put int as a struct member for example instead of variant, and you'll get the same warning. The thing is that variant initializes with the first value by default, and boost::blank is a spectial type to optimize the variant behavior. See the variant documentation in Boost

Upvotes: 0

Related Questions