gpt4 book ai didi

c++ - 是否存在静态警告?

转载 作者:IT老高 更新时间:2023-10-28 12:13:49 26 4
gpt4 key购买 nike

我知道this question其中提到了 Boost 的“STATIC WARNING”,但我想再问一次,具体来说,我如何实现一个 static_warning,它的操作类似于 static_assert 但只发出一个 警告在编译时而不是中止编译错误。

我想要类似于 Alexandrescu 在 C++11 之前提出的静态断言的建议,它以某种方式设法打印了一些有用的上下文信息作为错误的一部分。

要求用户启用某些标准编译器警告以使此构造起作用(可能是“无效的指针转换”或“违反严格的别名规则”)是可以接受的——任何应该是正常的警告的一部分编译反正可以用。

简而言之,我希望 static_warning(false, "Hello world"); 创建一个编译器警告,该警告应该以某种方式在警告消息中包含字符串“hello world”。这是否可能,比如在 GCC 和 MSVC 中,以及如何实现?

我很乐意为任何特别聪明的解决方案发放小额赏金。


作为一点解释:我在考虑 this question 时得到了这个想法。 :静态警告将是跟踪复杂模板特化的编译时过程的有用方法,否则很难调试。静态警告可以用作编译器发出“我现在正在编译这部分代码”的简单信标。


更新。理想情况下,警告将在以下设置中触发:

template <typename T> struct Foo
{
static_warning(std::is_pointer<T>::value, "Attempting to use pointer type.");
// ...
};

int main() { Foo<int> a; Foo<int*> b; }

最佳答案

播放迈克尔 E 的评论:

#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 PP_CAT(x,y) PP_CAT1(x,y)
#define PP_CAT1(x,y) x##y

namespace detail
{
struct true_type {};
struct false_type {};
template <int test> struct converter : public true_type {};
template <> struct converter<0> : public false_type {};
}

#define STATIC_WARNING(cond, msg) \
struct PP_CAT(static_warning,__LINE__) { \
DEPRECATE(void _(::detail::false_type const& ),msg) {}; \
void _(::detail::true_type const& ) {}; \
PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \
}

// Note: using STATIC_WARNING_TEMPLATE changes the meaning of a program in a small way.
// It introduces a member/variable declaration. This means at least one byte of space
// in each structure/class instantiation. STATIC_WARNING should be preferred in any
// non-template situation.
// 'token' must be a program-wide unique identifier.
#define STATIC_WARNING_TEMPLATE(token, cond, msg) \
STATIC_WARNING(cond, msg) PP_CAT(PP_CAT(_localvar_, token),__LINE__)

可以在命名空间、结构和函数范围内调用宏。给定输入:

#line 1
STATIC_WARNING(1==2, "Failed with 1 and 2");
STATIC_WARNING(1<2, "Succeeded with 1 and 2");

struct Foo
{
STATIC_WARNING(2==3, "2 and 3: oops");
STATIC_WARNING(2<3, "2 and 3 worked");
};

void func()
{
STATIC_WARNING(3==4, "Not so good on 3 and 4");
STATIC_WARNING(3<4, "3 and 4, check");
}

template <typename T> struct wrap
{
typedef T type;
STATIC_WARNING(4==5, "Bad with 4 and 5");
STATIC_WARNING(4<5, "Good on 4 and 5");
STATIC_WARNING_TEMPLATE(WRAP_WARNING1, 4==5, "A template warning");
};

template struct wrap<int>;

GCC 4.6(默认警告级别)产生:

static_warning.cpp: In constructor ‘static_warning1::static_warning1()’:static_warning.cpp:1:1: warning: ‘void static_warning1::_(const detail::false_type&)’     is deprecated (declared at static_warning.cpp:1): Failed with 1 and 2 [-Wdeprecated-declarations]static_warning.cpp: In constructor ‘Foo::static_warning6::static_warning6()’:static_warning.cpp:6:3: warning: ‘void Foo::static_warning6::_(const detail::false_type&)’    is deprecated (declared at static_warning.cpp:6): 2 and 3: oops [-Wdeprecated-declarations]static_warning.cpp: In constructor ‘func()::static_warning12::static_warning12()’:static_warning.cpp:12:3: warning: ‘void func()::static_warning12::_(const detail::false_type&)’     is deprecated (declared at static_warning.cpp:12): Not so good on 3 and 4 [-Wdeprecated-declarations]static_warning.cpp: In constructor ‘wrap<T>::static_warning19::static_warning19() [with T = int]’:static_warning.cpp:24:17:   instantiated from herestatic_warning.cpp:19:3: warning: ‘void wrap<T>::static_warning19::_(const detail::false_type&) [with T = int]’     is deprecated (declared at static_warning.cpp:19): Bad with 4 and 5 [-Wdeprecated-declarations]

虽然 Visual C++ 2010(在/W3 或更高版本)说:

warnproj.cpp(1): warning C4996: 'static_warning1::_': Failed with 1 and 2warnproj.cpp(1) : see declaration of 'static_warning1::_'warnproj.cpp(6): warning C4996: 'Foo::static_warning6::_': 2 and 3: oopswarnproj.cpp(6) : see declaration of 'Foo::static_warning6::_'warnproj.cpp(12): warning C4996: 'func::static_warning12::_': Not so good on 3 and 4warnproj.cpp(12) : see declaration of 'func::static_warning12::_'warnproj.cpp(19): warning C4996: 'wrap<T>::static_warning19::_': Bad with 4 and 5    with    [        T=int    ]warnproj.cpp(19) : see declaration of 'wrap<T>::static_warning19::_'    with    [        T=int    ]warnproj.cpp(19) : while compiling class template member function 'wrap<T>::static_warning19::static_warning19(void)'    with    [        T=int    ]warnproj.cpp(24) : see reference to class template instantiation 'wrap<T>::static_warning19' being compiled    with    [        T=int    ]

Linux 上的 Clang++ 3.1 可以产生更好的输出(颜色未显示):

tst3.cpp:1:1: warning: '_' is deprecated: Failed with 1 and 2      [-Wdeprecated-declarations]STATIC_WARNING(1==2, "Failed with 1 and 2");^tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'  PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \                                     ^tst3.cpp:6:3: warning: '_' is deprecated: 2 and 3: oops      [-Wdeprecated-declarations]  STATIC_WARNING(2==3, "2 and 3: oops");  ^tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'  PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \                                     ^tst3.cpp:12:3: warning: '_' is deprecated: Not so good on 3 and 4      [-Wdeprecated-declarations]  STATIC_WARNING(3==4, "Not so good on 3 and 4");  ^tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'  PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \                                     ^tst3.cpp:19:3: warning: '_' is deprecated: Bad with 4 and 5      [-Wdeprecated-declarations]  STATIC_WARNING(4==5, "Bad with 4 and 5");  ^tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'  PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \                                     ^tst3.cpp:23:17: note: in instantiation of member function      'wrap<int>::static_warning19::static_warning19' requested heretemplate struct wrap<int>                ^4 warnings generated.

关于c++ - 是否存在静态警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com