这个C4702链路时间警告是否有解决方法?

Rob*_*obH 14 c++ visual-studio-2010 compiler-warnings suppress-warnings

我正在使用boost :: variant并且在发布模式下编译时遇到问题.我在VC2010工作,警告级别为4,警告为错误.下面的代码在调试模式下编译很好,但是在发布模式下,我在链接时发出了一堆"无法访问的代码"C4702警告(可能是我在这里收到编译器警告,因为在启用优化时会生成链接时间代码.)

在这种情况下有没有人成功禁用这些警告?如果可能的话,我宁愿将高警告级别和警告保持为错误.

#pragma warning( disable:4702 )
Run Code Online (Sandbox Code Playgroud)

......似乎在这里不起作用.以下是一些示例代码:

#include <boost/variant.hpp>

struct null{};
typedef boost::variant< null, double > variant_t;

class addition_visitor
: public boost::static_visitor< variant_t >
{
public:
    template< typename T, typename U >
    variant_t operator()( const T&, const U& ) const
    { 
        throw( "Bad types" );
    }

    variant_t operator()( const double& left, const double& right ) const
    {
        return variant_t( left * right );
    }
};

int main(int /*argc*/, char** /*argv*/)
{
    variant_t a( 3.0 ), b( 2.0 );
    variant_t c = boost::apply_visitor( addition_visitor(), a, b );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

警告由模板化的operator()触发,我用它来捕获将访问者应用于错误变体类型的尝试.

Rob*_*obH 1

吃了一顿午餐并散步后,我找到了一个虽然不太令人满意但有效的解决方法。我没有从访问者返回一个变体并引发错误,而是返回一个成功布尔值并存储结果,因此:

#include <boost/variant.hpp>

struct null{};
typedef boost::variant< null, double > variant_t;

class addition_visitor
: public boost::static_visitor< bool >
{
public:
    template< typename T, typename U >
    bool operator()( const T&, const U& )
    { 
        //throw( "Bad types" );
        return false;
    }

    bool operator()( const double& left, const double& right )
    {
        result = variant_t( left * right );
        return true;
    }

    variant_t result;
};

int main(int /*argc*/, char** /*argv*/)
{
    variant_t a( 3.0 ), b( 2.0 );
    addition_visitor v;
    if( !boost::apply_visitor( v, a, b ) )
    {
        throw( "Bad types" );
    }

    variant_t c = v.result;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)