简体   繁体   English

在boost :: variant中使用boost :: blank时的编译器警告

[英]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: 我收到以下代码的警告,如果我从变体中删除boost :: blank,该代码将消失:

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>          ]

That warning is rather dumb. 该警告相当愚蠢。 It warns that MSVC now does the right thing as opposed to some ancient version. 它警告说,MSVC现在做正确的事情,而不是某些古老版本。 You can turn it off with a pragma . 您可以使用pragma程序将其关闭。

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. 例如,尝试将int作为结构成员而不是variant,您将得到相同的警告。 The thing is that variant initializes with the first value by default, and boost::blank is a spectial type to optimize the variant behavior. 问题在于,默认情况下,变量会使用第一个值进行初始化,而boost :: blank是用于优化变量行为的特殊类型。 See the variant documentation in Boost 请参阅Boost中的变体文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM