在Visual Studio 2008中设置编译时警告,类似"static_if"?
Set compile-time warnings in Visual Studio 2008 with "static_if"-like sulution?
我有一个大宏,其中包含大量的代码字符串,即从一种类型转换到另一种类型。当对结构声明中的初始类型进行一些更改时,可能会将其从大类型转换为小类型。然后编译器开始警告:
: '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>(); }
相关文章:
- 如何在c++中使用引用实现类似python的行为
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 使用Qt C++计算类似Git的SHA1哈希
- 类似枚举的计算常量
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 类似于strcat()的函数出现问题
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std中有类似find_last_of的函数,而string中没有
- std::ranges::elements_view,用于自定义类似元组的数据
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- C++win32 API创建多个类似视口的窗口
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- 算术运算的结果类似于:C浮点变量中的1/3
- 在没有未定义行为的情况下实现类似std::vector的容器
- Windows 进程间同步类似事件?
- 收到错误"invalid use of non-static data member 'stu::n' "
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 视觉 如何循环访问C++中遵循类似格式的多个.txt文件?
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- boost::bind()类似的东西,但用于函数调用