在Visual Studio 2008中设置编译时警告,类似"static_if"?

Set compile-time warnings in Visual Studio 2008 with "static_if"-like sulution?

本文关键字:类似 static if 警告 Studio Visual 2008 编译 设置      更新时间:2023-10-16

我有一个大宏,其中包含大量的代码字符串,即从一种类型转换到另一种类型。当对结构声明中的初始类型进行一些更改时,可能会将其从大类型转换为小类型。然后编译器开始警告:

: 'argument':截断常量

在我的代码中指向宏的行号,没有告诉真正的字符串和参数名称。我想写一个静态编译时hack,其中msg将被转换字段名:

#if defined(__GNUC__)
#   define DEPRECATE(foo, msg) foo __attribute__((deprecated(msg)))
#elif defined(_MSC_VER)
#   define DEPRECATE(foo, msg) __declspec(deprecated(msg)) foo
#else
#   error This compiler is not supported
#endif
#define STATIC_WARNING(name, expr, msg)         
    {                                           
        struct expr##__ {                       
        DEPRECATE(void name##(), msg) {}        
        expr##__() { name##(); }                
     }; }

,但首先我需要有类似"static_if"的东西。我的意思是我应该比较初始和最终类型,如果它们不相等,我将使用上面提到的STATIC_WARNING。有可能在MS Visual Studio 2008中编写类似的东西吗?顺便说一下,它不支持任何来自c++ 0x的东西。所以我甚至不能使用BOOstrongTATIC_ASSERT_MSG,尽管它需要c++ 0x来启用消息。

事实上,Andrew建议我使用模板专门化:

#if defined(__GNUC__)
#   define DEPRECATE(foo, msg) foo __attribute__((deprecated(msg)))
#elif defined(_MSC_VER)
#   define DEPRECATE(foo, msg) __declspec(deprecated(msg)) foo
#else
#   error This compiler is not supported
#endif
template<typename T, bool> struct UseDeprecated { UseDeprecated() { T()._(); } };
template<typename T> struct UseDeprecated<T, true> { };
#define STATIC_WARNING(condition, expr, msg)    
     {  struct expr##__ {                       
        DEPRECATE(void _(), msg) {}             
     }; UseDeprecated<expr##__, condition>(); }